1 | import { Set } from "immutable";
|
2 | import {
|
3 | identifier,
|
4 | TSUnionType,
|
5 | TSType,
|
6 | TSTypeReference,
|
7 | TSTypeParameterInstantiation,
|
8 | TSLiteralType,
|
9 | stringLiteral
|
10 | } from "@babel/types";
|
11 |
|
12 | const GenericType = (name: string, ...types: TSType[]): TSType =>
|
13 | TSTypeReference(identifier(name), TSTypeParameterInstantiation(types) as any);
|
14 |
|
15 | export const MaybeType = (type: TSType): TSType => GenericType("Maybe", type);
|
16 |
|
17 | export const PartialType = (type: TSType): TSType =>
|
18 | GenericType("Partial", type);
|
19 |
|
20 | export 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 |
|
32 | export const OptionalType = (type: TSType): TSType =>
|
33 | GenericType("Optional", type);
|
34 |
|
35 | export const OperationType = (type: TSType): TSType =>
|
36 | GenericType("Operation", type);
|
37 |
|
38 | export const ByIdType = (type: TSType): TSType => GenericType("ById", type);
|