-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.ts
174 lines (153 loc) · 3.37 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* istanbul ignore file */
import * as rt from 'runtypes';
/**
* Types that don't need any extra configuration
*/
const simpleTypeRt = rt.Record({
kind: rt.Union(
rt.Literal('boolean'),
rt.Literal('function'),
rt.Literal('never'),
rt.Literal('null'),
rt.Literal('number'),
rt.Literal('string'),
rt.Literal('symbol'),
rt.Literal('undefined'),
rt.Literal('unknown'),
),
});
export type SimpleType = rt.Static<typeof simpleTypeRt>;
/**
* Same as:
* rt.Literal(value)
*/
const literalTypeRt = rt.Record({
kind: rt.Literal('literal'),
value: rt.Union(rt.Boolean, rt.Null, rt.Number, rt.String, rt.Undefined),
});
export type LiteralType = rt.Static<typeof literalTypeRt>;
/**
* Same as using an already defined runtype. such as how "personRt" is used here::
*
* const personRt = rt.Record({ name: rt.String });
* const people = rt.Array(personRt);
*
*/
const namedTypeRt = rt.Record({
kind: rt.Literal('named'),
name: rt.String,
});
export type NamedType = rt.Static<typeof namedTypeRt>;
export type RecordField = {
name: string;
type: AnyType;
readonly?: boolean;
nullable?: boolean;
comment?: string | string[];
};
export type RecordType = {
kind: 'record';
fields: RecordField[];
};
/**
* Same as rt.Record({...})
*/
const recordTypeRt: rt.Runtype<RecordType> = rt.Lazy(() =>
rt.Record({
kind: rt.Literal('record'),
fields: rt.Array(
rt.Record({
name: rt.String,
readonly: rt.Boolean,
nullable: rt.Boolean,
type: anyTypeRt,
}),
),
}),
);
export type ArrayType = {
kind: 'array';
type: AnyType;
readonly?: boolean;
};
/**
* Same as rt.Array(type)
*/
const arrayTypeRt: rt.Runtype<ArrayType> = rt.Lazy(() =>
rt.Record({
kind: rt.Literal('array'),
type: anyTypeRt,
readonly: rt.Boolean,
}),
);
export type DictionaryType = {
kind: 'dictionary';
valueType: AnyType;
};
/**
* Same as rt.Dictionary(valueType)
*/
const dictionaryTypeRt: rt.Runtype<DictionaryType> = rt.Lazy(() =>
rt.Record({
kind: rt.Literal('dictionary'),
valueType: anyTypeRt,
}),
);
export type UnionType = {
kind: 'union';
types: AnyType[];
};
/**
* Same as rt.Union(type1, type2)
*/
const unionTypeRt: rt.Runtype<UnionType> = rt.Lazy(() =>
rt.Record({
kind: rt.Literal('union'),
types: rt
.Array(anyTypeRt)
.withConstraint(
(e) =>
e.length <= 20 || `Union can have at most 20 types. Got ${e.length}.`,
),
}),
);
export type IntersectionType = {
kind: 'intersect';
types: AnyType[];
};
/**
* Same as rt.Intersect(type1, type2)
*/
const intersectionTypeRt: rt.Runtype<IntersectionType> = rt.Lazy(() =>
rt.Record({
kind: rt.Literal('intersect'),
types: rt
.Array(anyTypeRt)
.withConstraint(
(e) =>
e.length <= 10 ||
`Intersection can have at most 10 types. Got ${e.length}.`,
),
}),
);
const anyTypeRt = rt.Union(
arrayTypeRt,
dictionaryTypeRt,
intersectionTypeRt,
literalTypeRt,
namedTypeRt,
recordTypeRt,
simpleTypeRt,
unionTypeRt,
);
export type AnyType = rt.Static<typeof anyTypeRt>;
export const rootTypeRt = rt.Intersect(
rt.Record({ name: rt.String, type: anyTypeRt }),
rt
.Record({
export: rt.Boolean,
comment: rt.Union(rt.String, rt.Array(rt.String)),
})
.asPartial(),
);
export type RootType = rt.Static<typeof rootTypeRt>;