Add support for $Shape

master
Paul Loyd 2017-11-29 01:06:10 +03:00
parent 31c8845a31
commit c299ef0f06
3 changed files with 51 additions and 0 deletions

View File

@ -71,6 +71,28 @@ function stripMaybe(params: (?Type)[], resolve: TypeId => Type): ?Type {
return clone(maybe.value);
}
function shape(params: (?Type)[], resolve: TypeId => Type): ?Type {
invariant(params.length === 1);
const [ref] = params;
invariant(ref && ref.kind === 'reference');
const record = resolve(ref.to);
invariant(record.kind === 'record');
const fields = wu(record.fields)
.map(clone)
.tap(field => field.required = false)
.toArray();
return {
kind: 'record',
fields,
};
}
export default {
Object: object,
Buffer: buffer,
@ -79,4 +101,5 @@ export default {
$PropertyType: elemType,
$ElementType: elemType,
$NonMaybeType: stripMaybe,
$Shape: shape,
};

8
tests/samples/shape.js Normal file
View File

@ -0,0 +1,8 @@
type X = {
x: string;
y: boolean;
}
type Y = $Shape<X>;
export type {Y};

20
tests/samples/shape.json Normal file
View File

@ -0,0 +1,20 @@
{
"types": [
{
"id": ["shape", "X"],
"kind": "record",
"fields": [
{"name": "x", "value": {"kind": "string"}, "required": true},
{"name": "y", "value": {"kind": "boolean"}, "required": true}
]
},
{
"id": ["shape", "Y"],
"kind": "record",
"fields": [
{"name": "x", "value": {"kind": "string"}, "required": false},
{"name": "y", "value": {"kind": "boolean"}, "required": false}
]
}
]
}