Add support for $Values

master
Paul Loyd 2017-11-29 10:08:20 +03:00
parent addbcb675b
commit 3cc324d1da
3 changed files with 53 additions and 1 deletions

View File

@ -117,6 +117,30 @@ function keys(params: (?Type)[], resolve: TypeId => Type): ?Type {
.map(name => ({kind: 'literal', value: name}))
.toArray();
// TODO: empty records.
return {
kind: 'union',
variants,
};
}
function values(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 variants = wu(record.fields).pluck('value').toArray();
// TODO: empty records.
// TODO: dedup values.
return {
kind: 'union',
variants,
@ -135,7 +159,7 @@ export default {
$ReadOnly: unwrap,
$Exact: unwrap, // TODO: another semantic for exact types?
$Keys: keys,
// TODO: $Values
$Values: values,
// TODO: $Diff
// TODO: $All
// TODO: $Either

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

@ -0,0 +1,8 @@
type X = {
a: string,
b: boolean,
};
type Y = $Values<X>;
export {Y};

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

@ -0,0 +1,20 @@
{
"types": [
{
"id": ["values", "X"],
"kind": "record",
"fields": [
{"name": "a", "value": {"kind": "string"}, "required": true},
{"name": "b", "value": {"kind": "boolean"}, "required": true}
]
},
{
"id": ["values", "Y"],
"kind": "union",
"variants": [
{"kind": "string"},
{"kind": "boolean"}
]
}
]
}