Add support for $Shape
parent
31c8845a31
commit
c299ef0f06
|
@ -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,
|
||||
};
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
type X = {
|
||||
x: string;
|
||||
y: boolean;
|
||||
}
|
||||
|
||||
type Y = $Shape<X>;
|
||||
|
||||
export type {Y};
|
|
@ -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}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue