From 08dc16aa4b794fd0b924679e0aabc2b8a58854bf Mon Sep 17 00:00:00 2001 From: Paul Loyd Date: Wed, 29 Nov 2017 01:16:29 +0300 Subject: [PATCH] Add support for $ReadOnly and $Exact --- src/globals.js | 13 +++++++++++++ tests/samples/exact.js | 10 ++++++++++ tests/samples/exact.json | 24 ++++++++++++++++++++++++ tests/samples/readOnly.js | 10 ++++++++++ tests/samples/readOnly.json | 24 ++++++++++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 tests/samples/exact.js create mode 100644 tests/samples/exact.json create mode 100644 tests/samples/readOnly.js create mode 100644 tests/samples/readOnly.json diff --git a/src/globals.js b/src/globals.js index cb92be9..9d1046d 100644 --- a/src/globals.js +++ b/src/globals.js @@ -93,6 +93,14 @@ function shape(params: (?Type)[], resolve: TypeId => Type): ?Type { }; } +function unwrap(params: (?Type)[]): ?Type { + invariant(params.length === 1); + + const [type] = params; + + return type ? clone(type) : null; +} + export default { Object: object, Buffer: buffer, @@ -102,4 +110,9 @@ export default { $ElementType: elemType, $NonMaybeType: stripMaybe, $Shape: shape, + $ReadOnly: unwrap, + $Exact: unwrap, // TODO: another semantic for exact types? + // TODO: $Keys + // TODO: $Values + // TODO: $Diff }; diff --git a/tests/samples/exact.js b/tests/samples/exact.js new file mode 100644 index 0000000..e436c26 --- /dev/null +++ b/tests/samples/exact.js @@ -0,0 +1,10 @@ +type X = { + x: string; + y: boolean; +} + +type Y = $Exact; + +type Z = $Exact<{z: string}>; + +export type {Y, Z}; diff --git a/tests/samples/exact.json b/tests/samples/exact.json new file mode 100644 index 0000000..b399291 --- /dev/null +++ b/tests/samples/exact.json @@ -0,0 +1,24 @@ +{ + "types": [ + { + "id": ["exact", "X"], + "kind": "record", + "fields": [ + {"name": "x", "value": {"kind": "string"}, "required": true}, + {"name": "y", "value": {"kind": "boolean"}, "required": true} + ] + }, + { + "id": ["exact", "Y"], + "kind": "reference", + "to": ["exact", "X"] + }, + { + "id": ["exact", "Z"], + "kind": "record", + "fields": [ + {"name": "z", "value": {"kind": "string"}, "required": true} + ] + } + ] +} diff --git a/tests/samples/readOnly.js b/tests/samples/readOnly.js new file mode 100644 index 0000000..c5026a7 --- /dev/null +++ b/tests/samples/readOnly.js @@ -0,0 +1,10 @@ +type X = { + x: string; + y: boolean; +} + +type Y = $ReadOnly; + +type Z = $ReadOnly<{z: string}>; + +export type {Y, Z}; diff --git a/tests/samples/readOnly.json b/tests/samples/readOnly.json new file mode 100644 index 0000000..ef99e6b --- /dev/null +++ b/tests/samples/readOnly.json @@ -0,0 +1,24 @@ +{ + "types": [ + { + "id": ["readOnly", "X"], + "kind": "record", + "fields": [ + {"name": "x", "value": {"kind": "string"}, "required": true}, + {"name": "y", "value": {"kind": "boolean"}, "required": true} + ] + }, + { + "id": ["readOnly", "Y"], + "kind": "reference", + "to": ["readOnly", "X"] + }, + { + "id": ["readOnly", "Z"], + "kind": "record", + "fields": [ + {"name": "z", "value": {"kind": "string"}, "required": true} + ] + } + ] +}