combo: make JsonObject arrays/objects covariant (#1885)

Summary:
This is more general: a mutable type is a subtype of its corresponding
read-only type. Using the covariant form lets structures like `string[]`
be subtypes of `JsonObject`, as `T[]` is not (in general) a subtype of
`(T | U)[]` but is a subtype of `$ReadOnlyArray<T | U>`.

Test Plan:
Demonstration type-level tests added.

wchargin-branch: jsonobject-readonly
This commit is contained in:
William Chargin 2020-06-22 10:42:36 -07:00 committed by GitHub
parent ad6656d53e
commit fb669962a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -8,8 +8,8 @@ export type JsonObject =
| number
| boolean
| null
| JsonObject[]
| {[string]: JsonObject};
| $ReadOnlyArray<JsonObject>
| {+[string]: JsonObject};
export type ParseResult<+T> =
| {|+ok: true, +value: T|}

View File

@ -3,6 +3,14 @@
import * as C from "./combo";
describe("src/util/combo", () => {
describe("type JsonObject", () => {
it("includes compound structures of strict subtypes", () => {
// (requires covariance in the array/object clauses)
(x: string[]): C.JsonObject => x;
(x: {[string]: number}): C.JsonObject => x;
});
});
describe("primitives", () => {
describe("string", () => {
it("accepts strings", () => {