-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinfer.types.ts
804 lines (615 loc) · 12.1 KB
/
infer.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
import * as t from './mod.ts';
declare function assert<T>(value: T): void;
const NULL = null;
const NUMBER = 123;
const STRING = 'foobar';
// ---
// NULL
// ---
let z1 = t.null();
assert<t.null>(z1);
assert<t.Infer<typeof z1>>(NULL);
// ---
// CONSTANT
// ---
let c1 = t.constant(NUMBER);
type C1 = t.Infer<typeof c1>;
assert<t.constant<typeof NUMBER>>(c1);
assert<t.constant<123>>(c1);
assert<C1>(NUMBER);
// @ts-expect-error; wrong value
assert<C1>(NUMBER + NUMBER);
// @ts-expect-error; wrong type
assert<C1>(STRING);
// ---
// BOOLEAN
// ---
let b1 = t.boolean();
assert<t.boolean>(b1);
assert<t.Infer<typeof b1>>(true);
t.boolean({
// @ts-expect-error; wrong type
default: '123',
});
// ---
// STRING / basic
// ---
let s1 = t.string({ format: 'email' });
assert<t.string<string>>(s1);
assert<t.string>(s1);
// @ts-expect-error; not an enum
assert<t.string<''>>(s1);
declare let S1: t.Infer<typeof s1>;
assert<string>(S1);
// @ts-expect-error; not constant
assert<''>(S1);
// ---
// STRING / enum values
// ---
t.string({
// @ts-expect-error; string[]
enum: STRING,
});
t.string({
// @ts-expect-error; string[]
enum: [NUMBER],
});
let s2 = t.string({
enum: ['foo', 'bar'],
});
assert<t.string<'foo' | 'bar'>>(s2);
assert<t.string<string>>(s2);
assert<t.string>(s2);
// @ts-expect-error; incomplete enum
assert<t.string<'foo'>>(s2);
declare let S2: t.Infer<typeof s2>;
// @ts-expect-error; incomplete enum
assert<'foo'>(S2);
assert<'foo' | 'bar'>(S2);
assert<string>(S2);
// ---
// STRING / non-constant enum
// ---
declare let xyz: string[];
let s3 = t.string({ enum: xyz });
assert<t.string<string>>(s3);
assert<t.string>(s3);
declare let S3: t.Infer<typeof s3>;
assert<string>(S3);
// can be any string
assert<typeof S3>('foobar');
// ---
// NUMBER / basic
// ---
let n1 = t.number();
assert<t.number<number>>(n1);
assert<t.number>(n1);
declare let N1: t.Infer<typeof n1>;
assert<number>(N1);
// @ts-expect-error; not constant/enum
assert<123>(N1);
// ---
// INTEGER / basic
// ---
let n2 = t.integer();
assert<t.integer<number>>(n2);
assert<t.integer>(n2);
// @ts-expect-error; distinct types
assert<t.number>(n2);
declare let N2: t.Infer<typeof n2>;
assert<number>(N2);
// @ts-expect-error; not constant
assert<123>(N2);
// ---
// NUMBER / enum
// ---
let n3 = t.number({
enum: [1.11, 2.22, 3.33],
});
assert<t.number>(n3);
assert<t.number<number>>(n3);
assert<t.number<1.11 | 2.22 | 3.33>>(n3);
// @ts-expect-error; incomplete enum
assert<t.number<1.11>>(n3);
declare let N3: t.Infer<typeof n3>;
assert<number>(N3);
assert<1.11 | 2.22 | 3.33>(N3);
// @ts-expect-error; incomplete enum
assert<1.11 | 2.22>(N3);
// @ts-expect-error; incomplete enum
assert<1.11>(N3);
// @ts-expect-error; not in enum
assert<0>(N3);
// ---
// INTEGER / enum
// ---
let n4 = t.integer({
enum: [1, 2, 3],
});
assert<t.integer>(n4);
assert<t.integer<number>>(n4);
assert<t.integer<1 | 2 | 3>>(n4);
// @ts-expect-error; incomplete enum
assert<t.integer<1>>(n4);
declare let N4: t.Infer<typeof n4>;
assert<number>(N4);
assert<1 | 2 | 3>(N4);
// @ts-expect-error; incomplete enum
assert<1 | 2>(N4);
// @ts-expect-error; incomplete enum
assert<1>(N4);
// @ts-expect-error; not in enum
assert<0>(N4);
// ---
// OBJECT / basic
// ---
let o1 = t.object({
name: t.string(),
age: t.number(),
});
assert<
t.object<{
name: t.string<string>;
age: t.number<number>;
}>
>(o1);
assert<
t.object<{
name: t.Type;
age: t.Type;
}>
>(o1);
declare let O1: t.Infer<typeof o1>;
assert<{
name: string;
age: number;
}>(O1);
// ---
// Object / via properties
// ---
let p2 = {
name: t.string(),
age: t.integer(),
};
let o2 = t.object(p2, {
additionalProperties: false,
});
assert<t.object<typeof p2>>(o2);
type O2 = t.Infer<typeof o2>;
type P2 = t.Infer<typeof p2>;
declare let O2: O2;
declare let P2: P2;
// infer as same
assert<P2>(P2);
assert<O2>(P2);
assert<P2>(O2);
assert<{
name: string;
age: number;
}>(P2);
assert<{
name: string;
age: number;
}>(O2);
// ---
// Object / with enum property
// ---
let o3 = t.object({
name: t.string(),
age: t.integer({
minimum: 18,
}),
status: t.string({
enum: ['single', 'married', 'widowed', 'divorced', 'separated', 'partnership'],
}),
});
declare let O3: t.Infer<typeof o3>;
assert<{
name: string;
age: number;
status: 'single' | 'married' | 'widowed' | 'divorced' | 'separated' | 'partnership';
}>(O3);
assert<{
name: string;
age: number;
status: string; // generic match ok
}>(O3);
assert<typeof O3>({
name: STRING,
age: NUMBER,
status: 'single',
});
assert<{
name: string;
age: number;
status: 'single' | 'married';
}>(
// @ts-expect-error; incomplete enum
O3,
);
// @ts-expect-error; missing properties
assert<typeof O3>({
name: STRING,
});
assert<typeof O3>({
name: STRING,
age: NUMBER,
// @ts-expect-error; enum
status: STRING,
});
// ---
// OBJECT / empty / any
// ---
let o4 = t.object();
type O4 = t.Infer<typeof o4>;
declare let O4: O4;
// deno-lint-ignore ban-types
assert<{}>(O4);
assert<Record<string, unknown>>(O4);
assert<O4>({
//
});
assert<O4>({
foo: 123,
});
// ---
// OBJECT / `required` match keys
// ---
let _ = t.object({
name: t.string(),
age: t.integer(),
}, {
required: [
'name',
// @ts-expect-error; not a key
'foobar',
],
});
// ---
// OBJECT / optional keys
// ---
let o5 = t.object({
uid: t.optional(
t.integer(),
),
name: t.string(),
age: t.optional(
t.integer(),
),
});
declare let O5: t.Infer<typeof o5>;
assert<{
name: string;
uid?: number;
age?: number;
}>(O5);
assert<{
name: string;
uid: number; // <<
age?: number;
}>(
// @ts-expect-error; missing uid optional
O5,
);
assert<typeof O5>({
name: STRING,
});
assert<typeof O5>({
name: STRING,
age: NUMBER,
});
assert<typeof O5>({
name: STRING,
uid: NUMBER,
});
assert<typeof O5>({
name: STRING,
uid: NUMBER,
// @ts-expect-error; not number
age: STRING,
});
// ---
// OBJECT / readonly
// ---
let o6 = t.readonly(
t.object({
name: t.string(),
age: t.optional(
t.integer(),
),
}),
);
declare let O6: t.Infer<typeof o6>;
assert<{
readonly name: string;
readonly age?: number;
}>(O6);
// @ts-expect-error; readonly
O6.age = NUMBER;
// @ts-expect-error; readonly
O6.name = STRING;
// ---
// ENUM
// ---
// @ts-expect-error; must be array
t.enum(STRING, NUMBER, NULL);
// can be mixed types
let e1 = t.enum([STRING, NUMBER, NULL]);
assert<t.enum<typeof STRING | typeof NUMBER | typeof NULL>>(e1);
assert<t.enum<'foobar' | 123 | null>>(e1);
type E1 = t.Infer<typeof e1>;
assert<E1>(STRING);
assert<E1>(NUMBER);
assert<E1>(NULL);
// @ts-expect-error; not in enum
assert<E1>('howdy');
declare let E1: E1;
assert<typeof STRING | typeof NUMBER | typeof NULL>(E1);
assert<'foobar' | 123 | null>(E1);
// ---
// ARRAY / basic
// ---
let a1 = t.array(t.string());
type A1 = t.Infer<typeof a1>;
declare let A1: A1;
assert<string[]>(A1);
assert<A1>([]);
assert<A1>([STRING]);
// @ts-expect-error; not string[]
assert<A1>([NUMBER]);
// @ts-expect-error; not string[]
assert<A1>([] as unknown[]);
// ---
// ARRAY / readonly
// ---
let a2 = t.readonly(
t.array(t.string()),
);
type A2 = t.Infer<typeof a2>;
declare let A2: A2;
assert<readonly string[]>(A2);
// @ts-expect-error; missing `readonly`
assert<string[]>(A2);
// @ts-expect-error; cannot assign into readonly
A2[0] = STRING;
// ---
// ARRAY / with objects
// ---
let a3 = t.array(
t.object({
name: t.string(),
age: t.integer(),
}),
);
type A3 = t.Infer<typeof a3>;
declare let A3: A3;
assert<{
name: string;
age: number;
}[]>(A3);
// @ts-expect-error; not array
assert<A3>(NUMBER);
assert<A3>([{
name: STRING,
age: NUMBER,
}]);
assert<A3>([{
name: STRING,
// @ts-expect-error; type
age: STRING,
}]);
assert<A3>([
// @ts-expect-error; missing key
{ name: STRING },
// @ts-expect-error; missing key
{ age: NUMBER },
// @ts-expect-error; missing keys
{},
]);
// ---
// ARRAY / with readonly objects
// ---
let a4 = t.array(
t.readonly(
t.object({
name: t.string(),
age: t.integer(),
}),
),
);
type A4 = t.Infer<typeof a4>;
declare let A4: A4;
assert<{
readonly name: string;
readonly age: number;
}[]>(A4);
// @ts-expect-error; readonly object
A4[0].age = NUMBER;
// array itself is not readonly
A4[0] = { name: STRING, age: NUMBER };
assert<A4>([{
name: STRING,
age: NUMBER,
}]);
assert<A4>([{
name: STRING,
// @ts-expect-error; type
age: STRING,
}]);
assert<A4>([
// @ts-expect-error; missing key
{ name: STRING },
// @ts-expect-error; missing key
{ age: NUMBER },
// @ts-expect-error; missing keys
{},
]);
// ---
// ARRAY / readonly w/ objects
// ---
let a5 = t.readonly(
t.array(
t.object({
name: t.string(),
}),
),
);
type A5 = t.Infer<typeof a5>;
declare let A5: A5;
assert<readonly { name: string }[]>(A5);
// @ts-expect-error; readonly
A5[0] = { name: STRING };
// items arent readonly
A5[0].name = STRING;
// ---
// ARRAY / ENUM
// ---
let a6 = t.array(
t.enum(['UP', 'DOWN', 'LEFT', 'RIGHT']),
);
type A6 = t.Infer<typeof a6>;
declare let A6: A6;
// @ts-expect-error; incomplete
assert<('UP' | 'DOWN' | 'LEFT')[]>(A6);
assert<('UP' | 'DOWN' | 'LEFT' | 'RIGHT')[]>(A6);
// @ts-expect-error; invalid
assert<A6>(['ANOTHER']);
assert<A6>(['DOWN']);
assert<A6>([]);
// ---
// ARRAY / READONLY w/ ENUM
// ---
let a7 = t.readonly(a6);
type A7 = t.Infer<typeof a7>;
declare let A7: A7;
// @ts-expect-error; incomplete
assert<readonly ('UP' | 'DOWN' | 'LEFT')[]>(A7);
assert<readonly ('UP' | 'DOWN' | 'LEFT' | 'RIGHT')[]>(A7);
// @ts-expect-error; invalid
assert<A7>(['ANOTHER']);
assert<A7>(['DOWN']);
assert<A7>([]);
// ---
// TUPLE
// ---
// @ts-expect-error; must be array
t.tuple(t.nUMBER(), t.string());
let t1 = t.tuple([t.number(), t.string()]);
assert<t.tuple<[t.number<number>, t.string<string>]>>(t1);
assert<t.tuple<[t.number, t.string]>>(t1);
declare let T1: t.Infer<typeof t1>;
assert<[number, string]>(T1);
// ---
// TUPLE / with enum
// ---
let t2 = t.tuple([
t.enum(['NW', 'NE', 'SW', 'SE'], {
deprecated: true,
}),
t.boolean(),
]);
assert<t.tuple<[t.enum<'NW' | 'NE' | 'SW' | 'SE'>, t.boolean]>>(t2);
assert<t.tuple<[t.enum<string>, t.boolean]>>(t2);
declare let T2: t.Infer<typeof t2>;
assert<['NW' | 'NE' | 'SW' | 'SE', boolean]>(T2);
assert<[string, boolean]>(T2);
// @ts-expect-error; STRING is not in ENUM
assert<typeof T2>([STRING, true]);
// ---
// COMPOSITIONS
// ---
let not1 = t.not(t.string());
type NOT1 = t.Infer<typeof not1>;
declare let NOT1: NOT1;
assert<unknown>(NOT1);
let any1 = t.any(t.string(), t.number());
type ANY1 = t.Infer<typeof any1>;
declare let ANY1: ANY1;
assert<string | number>(ANY1);
assert<ANY1>(STRING);
assert<ANY1>(NUMBER);
// @ts-expect-error; must all be string
let all1 = t.all(t.string(), t.number());
type ALL1 = t.Infer<typeof all1>;
declare let ALL1: ALL1;
assert<string>(ALL1);
assert<ALL1>(STRING);
let one1 = t.one(t.string(), t.number(), t.boolean());
type ONE1 = t.Infer<typeof one1>;
declare let ONE1: ONE1;
assert<string | number | boolean>(ONE1);
assert<ONE1>(STRING);
assert<ONE1>(NUMBER);
assert<ONE1>(false);
assert<ONE1>(true);
// ---
// PARTIAL
// ---
let pa1 = t.partial(
t.object({
foo: t.string(),
bar: t.array(t.number()),
}),
);
type PA1 = t.Infer<typeof pa1>;
declare let PA1: PA1;
assert<{
foo?: string;
bar?: number[];
}>(PA1);
assert<PA1>({});
assert<PA1>({ foo: STRING });
let pa2 = t.partial(
t.object({
foo: t.string(),
bar: t.object({
name: t.string(),
age: t.number(),
}),
}),
);
type PA2 = t.Infer<typeof pa2>;
declare let PA2: PA2;
assert<{
foo?: string;
bar?: {
name: string;
age: number;
};
}>(PA2);
assert<PA2>({
foo: STRING,
});
assert<PA2>({
bar: {
name: STRING,
age: NUMBER,
},
});
assert<PA2>({
// @ts-expect-error missing keys
bar: {},
});
let _pa3 = t.partial(
// @ts-expect-error wrong type
t.string(),
);
// ---
// UNKNOWN
// ---
let u1 = t.unknown();
declare let U1: t.Infer<typeof u1>;
assert<unknown>(U1);
assert<unknown>(123);
// ---
// DICT
// ---
let d1 = t.dict(t.integer());
declare let D1: t.Infer<typeof d1>;
assert<{ [key: string]: number }>(D1);
assert<Record<string, number>>(D1);
let d2 = t.dict(
t.array(t.string()),
);
declare let D2: t.Infer<typeof d2>;
assert<Record<string, string[]>>(D2);