mirror of
https://github.com/status-im/sourcecred.git
synced 2025-02-24 02:08:09 +00:00
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:
parent
ad6656d53e
commit
fb669962a0
@ -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|}
|
||||
|
@ -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", () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user