Add support for $NonMaybeType

master
Paul Loyd 2017-11-29 00:57:22 +03:00
parent c95581329e
commit 31c8845a31
4 changed files with 43 additions and 2 deletions

View File

@ -1,6 +1,6 @@
import wu from 'wu';
import {invariant} from './utils';
import {invariant, clone} from './utils';
import type {Type, TypeId} from './types';
function object(params: (?Type)[]): ?Type {
@ -50,7 +50,25 @@ function elemType(params: (?Type)[], resolve: TypeId => Type): ?Type {
const field = wu(record.fields).find(field => field.name === key.value);
return field ? Object.assign({}, (field.value: $FlowFixMe)) : null;
// TODO: what about removing "id"?
return field ? clone(field.value) : null;
}
function stripMaybe(params: (?Type)[], resolve: TypeId => Type): ?Type {
invariant(params.length === 1);
const [ref] = params;
invariant(ref && ref.kind === 'reference');
const maybe = resolve(ref.to);
// TODO: support for unions and nested maybe.
if (maybe.kind !== 'maybe') {
return clone(ref);
}
return clone(maybe.value);
}
export default {
@ -60,4 +78,5 @@ export default {
$ReadOnlyArray: array,
$PropertyType: elemType,
$ElementType: elemType,
$NonMaybeType: stripMaybe,
};

View File

@ -9,3 +9,7 @@ export function last<T>(list: T[]): T {
return list[list.length - 1];
}
export function clone<T: Object>(obj: T): T {
return Object.assign({}, obj);
}

View File

@ -0,0 +1,5 @@
type X = ?string;
type Y = $NonMaybeType<X>;
export type {Y};

View File

@ -0,0 +1,13 @@
{
"types": [
{
"id": ["nonMaybeType", "X"],
"kind": "maybe",
"value": {"kind": "string"}
},
{
"id": ["nonMaybeType", "Y"],
"kind": "string"
}
]
}