UNPKG

1.01 kBPlain TextView Raw
1import { Set } from "immutable";
2import {
3 identifier,
4 TSUnionType,
5 TSType,
6 TSTypeReference,
7 TSTypeParameterInstantiation,
8 TSLiteralType,
9 stringLiteral
10} from "@babel/types";
11
12const GenericType = (name: string, ...types: TSType[]): TSType =>
13 TSTypeReference(identifier(name), TSTypeParameterInstantiation(types) as any);
14
15export const MaybeType = (type: TSType): TSType => GenericType("Maybe", type);
16
17export const PartialType = (type: TSType): TSType =>
18 GenericType("Partial", type);
19
20export const IfType = (possibleTypes: Set<string>, type: TSType): TSType =>
21 GenericType(
22 "If",
23 TSUnionType(
24 possibleTypes
25 .sort()
26 .toArray()
27 .map(type => TSLiteralType(stringLiteral(type)))
28 ),
29 type
30 );
31
32export const OptionalType = (type: TSType): TSType =>
33 GenericType("Optional", type);
34
35export const OperationType = (type: TSType): TSType =>
36 GenericType("Operation", type);
37
38export const ByIdType = (type: TSType): TSType => GenericType("ById", type);