This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
flow_tests.js
350 lines (303 loc) · 6.08 KB
/
flow_tests.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// @flow
import type {
ContextEntry,
Context,
ValidationError,
Validation,
Validate,
TypeOf,
Predicate,
Props
} from './src/index'
import {
Type,
LiteralType,
InstanceOfType,
ClassType,
ArrayType,
UnionType,
TupleType,
IntersectionType,
MaybeType,
MappingType,
RefinementType,
$ExactType,
ObjectType,
} from './src/index'
import * as t from './src/index'
//
// irreducibles
//
const T1 = t.number
t.map(v1 => {
(v1: number)
;(v1: TypeOf<typeof T1>)
// $ExpectError
;(v1: string)
}, t.validate(1, T1))
// $ExpectError
;('a': TypeOf<typeof T1>)
// runtime type introspection
const RTI1 = t.number
;(RTI1.name: string)
//
// instanceOf
//
class A {}
const T2 = t.instanceOf(A)
t.map(v2 => {
(v2: A)
;(v2: TypeOf<typeof T2>)
// $ExpectError
;(v2: string)
}, t.validate(new A(), T2))
// $ExpectError
;(1: TypeOf<typeof T2>)
// runtime type introspection
const RTI2 = t.instanceOf(A)
;(RTI2.name: string)
;(RTI2.ctor: Class<A>)
//
// literals
//
const T3 = t.literal('a')
t.map(v3 => {
(v3: 'a')
;(v3: TypeOf<typeof T3>)
// $ExpectError
;(v3: 'b')
}, t.validate('a', T3))
// $ExpectError
;(1: TypeOf<typeof T3>)
;('b': TypeOf<typeof T3>)
// runtime type introspection
const RTI3 = t.literal('a')
;(RTI3.name: string)
;(RTI3.value: string)
//
// arrays
//
const T4 = t.array(t.number)
t.map(v4 => {
(v4: Array<number>)
;(v4: TypeOf<typeof T4>)
// $ExpectError
;(v4: Array<string>)
}, t.validate([1, 2, 3], T4))
// $ExpectError
;(1: TypeOf<typeof T4>)
// $ExpectError
;(['a']: TypeOf<typeof T4>)
// runtime type introspection
const RTI4 = t.array(t.object({ a: t.number }))
;(RTI4.name: string)
;(RTI4.type: Type<{ a: number }>)
//
// unions
//
const T5 = t.union([t.string, t.number])
t.map(v5 => {
(v5: string | number)
;(v5: TypeOf<typeof T5>)
// $ExpectError
;(v5: string)
}, t.validate(1, T5))
// $ExpectError
;(true: TypeOf<typeof T5>)
// runtime type introspection
const RTI5 = t.union([t.string, t.object({ a: t.number })])
;(RTI5.name: string)
;(RTI5.types[0]: Type<string>)
;(RTI5.types[1]: Type<{ a: number }>)
//
// tuples
//
const T6 = t.tuple([t.string, t.number])
t.map(v6 => {
(v6: [string, number])
;(v6: TypeOf<typeof T6>)
// $ExpectError
;(v6: [number, number])
}, t.validate(['a', 1], T6))
// $ExpectError
;([1, 2]: TypeOf<typeof T6>)
// runtime type introspection
const RTI6 = t.tuple([t.string, t.object({ a: t.number })])
;(RTI6.name: string)
;(RTI6.types[0]: Type<string>)
;(RTI6.types[1]: Type<{ a: number }>)
//
// intersections
//
// $ExpectError
t.intersection()
// $ExpectError
t.intersection([])
const T7 = t.intersection([t.object({ a: t.number }), t.object({ b: t.number })])
t.map(v7 => {
(v7: { a: number } & { b: number })
;(v7: TypeOf<typeof T7>)
;(v7: { a: number })
;(v7: { b: number })
// $ExpectError
;(v7: { a: string })
}, t.validate({ a: 1, b: 2 }, T7))
// $ExpectError
;(1: TypeOf<typeof T7>)
// runtime type introspection
const RTI7 = t.intersection([t.object({ a: t.number }), t.object({ b: t.number })])
;(RTI7.name: string)
;(RTI7.types[0]: Type<{ a: number }>)
;(RTI7.types[1]: Type<{ b: number }>)
//
// maybes
//
const T8 = t.maybe(t.number)
t.map(v8 => {
(v8: ?number)
;(v8: TypeOf<typeof T8>)
// $ExpectError
;(v8: ?string)
}, t.validate(null, T8))
;(null: TypeOf<typeof T8>)
;(undefined: TypeOf<typeof T8>)
// $ExpectError
;('a': TypeOf<typeof T8>)
// runtime type introspection
const RTI8 = t.maybe(t.object({ a: t.number }))
;(RTI8.name: string)
;(RTI8.type: Type<{ a: number }>)
//
// mappings
//
const T9 = t.mapping(t.union([t.literal('a'), t.literal('b')]), t.number)
t.map(v9 => {
(v9: { [key: 'a' | 'b']: number })
;(v9: TypeOf<typeof T9>)
// $ExpectError
;(v9: { [key: string]: number })
}, t.validate(null, T9))
;({}: TypeOf<typeof T9>)
// $ExpectError
;(1: TypeOf<typeof T9>)
// runtime type introspection
const RTI9 = t.mapping(t.union([t.literal('a'), t.literal('b')]), t.object({ a: t.number }))
;(RTI9.name: string)
;(RTI9.domain: Type<'a' | 'b'>)
;(RTI9.codomain: Type<{ a: number }>)
//
// refinements
//
const T10 = t.refinement(t.number, n => n >= 0)
t.map(v10 => {
(v10: number)
;(v10: TypeOf<typeof T10>)
// $ExpectError
;(v10: string)
}, t.validate(1, T10))
// $ExpectError
;('a': TypeOf<typeof T10>)
// runtime type introspection
const RTI10 = t.refinement(t.object({ a: t.number }), () => true)
;(RTI10.name: string)
;(RTI10.type: Type<{ a: number }>)
//
// recursive types
//
type T11T = {
a: number,
b: ?T11T
};
const T11 = t.recursion('T11', self => t.object({
a: t.number,
b: t.maybe(self)
}))
t.map(v11 => {
(v11: T11T)
;(v11: TypeOf<typeof T11>)
// $ExpectError
;(v11: string)
}, t.validate({ a: 1 }, T11))
// $ExpectError
;(1: TypeOf<typeof T11>)
// runtime type introspection
const RTI11 = t.recursion('T11', self => t.object({
a: t.number,
b: t.maybe(self)
}))
;(RTI11.name: string)
//
// $Exact
//
const T13 = t.$exact({ a: t.number })
t.map(v13 => {
(v13: {| a: number |})
;(v13: TypeOf<typeof T13>)
// $ExpectError
;(v13: number)
}, t.validate(1, T13))
// $ExpectError
;(1: TypeOf<typeof T13>)
// runtime type introspection
const RTI13 = t.$exact({ a: t.number })
;(RTI13.name: string)
;(RTI13.props: Props)
;(RTI13.props.a: Type<number>)
//
// objects
//
type T15T = {
a: number,
b: {
c: string,
d: {
e: number
}
}
};
const T15 = t.object({
a: t.number,
b: t.object({
c: t.string,
d: t.object({
e: t.number
})
})
})
t.map(v15 => {
(v15: T15T)
;(v15: TypeOf<typeof T15>)
// $ExpectError
;(v15.b.d.e: string)
}, t.validate({}, T15))
// $ExpectError
;(1: TypeOf<typeof T15>)
// $ExpectError
;({}: TypeOf<typeof T15>)
const RTI15 = t.object({
a: t.number,
b: t.object({
c: t.string,
d: t.object({
e: t.number
})
})
})
;(RTI15.name: string)
;(RTI15.props: Props)
//
// classOf
//
const T16 = t.classOf(A)
t.map(v16 => {
(v16: Class<A>)
;(v16: TypeOf<typeof T16>)
// $ExpectError
;(v16: string)
}, t.validate(A, T16))
// $ExpectError
;(1: TypeOf<typeof T16>)
// runtime type introspection
const RTI16 = t.classOf(A)
;(RTI16.name: string)
;(RTI16.ctor: Class<A>)