-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathtemplateLiteralTypes1.types
596 lines (459 loc) · 21.3 KB
/
templateLiteralTypes1.types
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
=== tests/cases/conformance/types/literal/templateLiteralTypes1.ts ===
// Template types example from #12754
const createScopedActionType = <S extends string>(scope: S) => <T extends string>(type: T) => `${scope}/${type}` as `${S}/${T}`;
>createScopedActionType : <S extends string>(scope: S) => <T extends string>(type: T) => `${S}/${T}`
><S extends string>(scope: S) => <T extends string>(type: T) => `${scope}/${type}` as `${S}/${T}` : <S extends string>(scope: S) => <T extends string>(type: T) => `${S}/${T}`
>scope : S
><T extends string>(type: T) => `${scope}/${type}` as `${S}/${T}` : <T extends string>(type: T) => `${S}/${T}`
>type : T
>`${scope}/${type}` as `${S}/${T}` : `${S}/${T}`
>`${scope}/${type}` : `${S}/${T}`
>scope : S
>type : T
const createActionInMyScope = createScopedActionType("MyScope"); // <T extends string>(type: T) => `MyScope/${T}`
>createActionInMyScope : <T extends string>(type: T) => `MyScope/${T}`
>createScopedActionType("MyScope") : <T extends string>(type: T) => `MyScope/${T}`
>createScopedActionType : <S extends string>(scope: S) => <T extends string>(type: T) => `${S}/${T}`
>"MyScope" : "MyScope"
const MY_ACTION = createActionInMyScope("MY_ACTION"); // 'MyScope/MY_ACTION'
>MY_ACTION : "MyScope/MY_ACTION"
>createActionInMyScope("MY_ACTION") : "MyScope/MY_ACTION"
>createActionInMyScope : <T extends string>(type: T) => `MyScope/${T}`
>"MY_ACTION" : "MY_ACTION"
// Union types are distributed over template types
type EventName<S extends string> = `${S}Changed`;
>EventName : `${S}Changed`
type EN1 = EventName<'Foo' | 'Bar' | 'Baz'>;
>EN1 : "FooChanged" | "BarChanged" | "BazChanged"
type Loc = `${'top' | 'middle' | 'bottom'}-${'left' | 'center' | 'right'}`;
>Loc : "top-left" | "top-center" | "top-right" | "middle-left" | "middle-center" | "middle-right" | "bottom-left" | "bottom-center" | "bottom-right"
// Primitive literal types can be spread into templates
type ToString<T extends string | number | boolean | bigint> = `${T}`;
>ToString : `${T}`
type TS1 = ToString<'abc' | 42 | true | -1234n>;
>TS1 : "abc" | "true" | "42" | "-1234"
>true : true
>-1234n : -1234n
>1234n : 1234n
// Nested template literal type normalization
type TL1<T extends string> = `a${T}b${T}c`;
>TL1 : `a${T}b${T}c`
type TL2<U extends string> = TL1<`x${U}y`>; // `ax${U}ybx{$U}yc`
>TL2 : `ax${U}ybx${U}yc`
type TL3 = TL2<'o'>; // 'axoybxoyc'
>TL3 : "axoybxoyc"
// Casing intrinsics
type Cases<T extends string> = `${Uppercase<T>} ${Lowercase<T>} ${Capitalize<T>} ${Uncapitalize<T>}`;
>Cases : `${Uppercase<T>} ${Lowercase<T>} ${Capitalize<T>} ${Uncapitalize<T>}`
type TCA1 = Cases<'bar'>; // 'BAR bar Bar bar'
>TCA1 : "BAR bar Bar bar"
type TCA2 = Cases<'BAR'>; // 'BAR bar BAR bAR'
>TCA2 : "BAR bar BAR bAR"
// Assignability
function test<T extends 'foo' | 'bar'>(name: `get${Capitalize<T>}`) {
>test : <T extends "bar" | "foo">(name: `get${Capitalize<T>}`) => void
>name : `get${Capitalize<T>}`
let s1: string = name;
>s1 : string
>name : "getFoo" | "getBar"
let s2: 'getFoo' | 'getBar' = name;
>s2 : "getFoo" | "getBar"
>name : "getFoo" | "getBar"
}
function fa1<T>(x: T, y: { [P in keyof T]: T[P] }, z: { [P in keyof T & string as `p_${P}`]: T[P] }) {
>fa1 : <T>(x: T, y: { [P in keyof T]: T[P]; }, z: { [P in keyof T & string as `p_${P}`]: T[P]; }) => void
>x : T
>y : { [P in keyof T]: T[P]; }
>z : { [P in keyof T & string as `p_${P}`]: T[P]; }
y = x;
>y = x : T
>y : { [P in keyof T]: T[P]; }
>x : T
z = x; // Error
>z = x : T
>z : { [P in keyof T & string as `p_${P}`]: T[P]; }
>x : T
}
function fa2<T, U extends T, A extends string, B extends A>(x: { [P in B as `p_${P}`]: T }, y: { [Q in A as `p_${Q}`]: U }) {
>fa2 : <T, U extends T, A extends string, B extends A>(x: { [P in B as `p_${P}`]: T; }, y: { [Q in A as `p_${Q}`]: U; }) => void
>x : { [P in B as `p_${P}`]: T; }
>y : { [Q in A as `p_${Q}`]: U; }
x = y;
>x = y : { [Q in A as `p_${Q}`]: U; }
>x : { [P in B as `p_${P}`]: T; }
>y : { [Q in A as `p_${Q}`]: U; }
y = x; // Error
>y = x : { [P in B as `p_${P}`]: T; }
>y : { [Q in A as `p_${Q}`]: U; }
>x : { [P in B as `p_${P}`]: T; }
}
// String transformations using recursive conditional types
type Join<T extends unknown[], D extends string> =
>Join : Join<T, D>
T extends [] ? '' :
T extends [string | number | boolean | bigint] ? `${T[0]}` :
T extends [string | number | boolean | bigint, ...infer U] ? `${T[0]}${D}${Join<U, D>}` :
string;
type TJ1 = Join<[1, 2, 3, 4], '.'>
>TJ1 : "1.2.3.4"
type TJ2 = Join<['foo', 'bar', 'baz'], '-'>;
>TJ2 : "foo-bar-baz"
type TJ3 = Join<[], '.'>
>TJ3 : ""
// Inference based on delimiters
type MatchPair<S extends string> = S extends `[${infer A},${infer B}]` ? [A, B] : unknown;
>MatchPair : MatchPair<S>
type T20 = MatchPair<'[1,2]'>; // ['1', '2']
>T20 : ["1", "2"]
type T21 = MatchPair<'[foo,bar]'>; // ['foo', 'bar']
>T21 : ["foo", "bar"]
type T22 = MatchPair<' [1,2]'>; // unknown
>T22 : unknown
type T23 = MatchPair<'[123]'>; // unknown
>T23 : unknown
type T24 = MatchPair<'[1,2,3,4]'>; // ['1', '2,3,4']
>T24 : ["1", "2,3,4"]
type SnakeToCamelCase<S extends string> =
>SnakeToCamelCase : SnakeToCamelCase<S>
S extends `${infer T}_${infer U}` ? `${Lowercase<T>}${SnakeToPascalCase<U>}` :
S extends `${infer T}` ? `${Lowercase<T>}` :
SnakeToPascalCase<S>;
type SnakeToPascalCase<S extends string> =
>SnakeToPascalCase : SnakeToPascalCase<S>
string extends S ? string :
S extends `${infer T}_${infer U}` ? `${Capitalize<Lowercase<T>>}${SnakeToPascalCase<U>}` :
S extends `${infer T}` ? `${Capitalize<Lowercase<T>>}` :
never;
type RR0 = SnakeToPascalCase<'hello_world_foo'>; // 'HelloWorldFoo'
>RR0 : "HelloWorldFoo"
type RR1 = SnakeToPascalCase<'FOO_BAR_BAZ'>; // 'FooBarBaz'
>RR1 : "FooBarBaz"
type RR2 = SnakeToCamelCase<'hello_world_foo'>; // 'helloWorldFoo'
>RR2 : "helloWorldFoo"
type RR3 = SnakeToCamelCase<'FOO_BAR_BAZ'>; // 'fooBarBaz'
>RR3 : "fooBarBaz"
// Single character inference
type FirstTwoAndRest<S extends string> = S extends `${infer A}${infer B}${infer R}` ? [`${A}${B}`, R] : unknown;
>FirstTwoAndRest : FirstTwoAndRest<S>
type T25 = FirstTwoAndRest<'abcde'>; // ['ab', 'cde']
>T25 : ["ab", "cde"]
type T26 = FirstTwoAndRest<'ab'>; // ['ab', '']
>T26 : ["ab", ""]
type T27 = FirstTwoAndRest<'a'>; // unknown
>T27 : unknown
type HexDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' |'8' | '9' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f';
>HexDigit : HexDigit
type HexColor<S extends string> =
>HexColor : HexColor<S>
S extends `#${infer R1}${infer R2}${infer G1}${infer G2}${infer B1}${infer B2}` ?
[R1, R2, G1, G2, B1, B2] extends [HexDigit, HexDigit, HexDigit, HexDigit, HexDigit, HexDigit] ?
S :
never :
never;
type TH1 = HexColor<'#8080FF'>; // '#8080FF'
>TH1 : "#8080FF"
type TH2 = HexColor<'#80c0ff'>; // '#80c0ff'
>TH2 : "#80c0ff"
type TH3 = HexColor<'#8080F'>; // never
>TH3 : never
type TH4 = HexColor<'#8080FFF'>; // never
>TH4 : never
// Recursive inference
type Trim<S extends string> =
>Trim : Trim<S>
S extends ` ${infer T}` ? Trim<T> :
S extends `${infer T} ` ? Trim<T> :
S;
type TR1 = Trim<'xx '>; // 'xx'
>TR1 : "xx"
type TR2 = Trim<' xx'>; // 'xx'
>TR2 : "xx"
type TR3 = Trim<' xx '>; // 'xx'
>TR3 : "xx"
type Split<S extends string, D extends string> =
>Split : Split<S, D>
string extends S ? string[] :
S extends '' ? [] :
S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] :
[S];
type T40 = Split<'foo', '.'>; // ['foo']
>T40 : ["foo"]
type T41 = Split<'foo.bar.baz', '.'>; // ['foo', 'bar', 'baz']
>T41 : ["foo", "bar", "baz"]
type T42 = Split<'foo.bar', ''>; // ['f', 'o', 'o', '.', 'b', 'a', 'r']
>T42 : ["f", "o", "o", ".", "b", "a", "r"]
type T43 = Split<any, '.'>; // string[]
>T43 : string[]
// Inference and property name paths
declare function getProp<T, P0 extends keyof T & string, P1 extends keyof T[P0] & string, P2 extends keyof T[P0][P1] & string>(obj: T, path: `${P0}.${P1}.${P2}`): T[P0][P1][P2];
>getProp : { <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string, P2 extends keyof T[P0][P1] & string>(obj: T, path: `${P0}.${P1}.${P2}`): T[P0][P1][P2]; <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string>(obj: T, path: `${P0}.${P1}`): T[P0][P1]; <T, P0 extends keyof T & string>(obj: T, path: P0): T[P0]; (obj: object, path: string): unknown; }
>obj : T
>path : `${P0}.${P1}.${P2}`
declare function getProp<T, P0 extends keyof T & string, P1 extends keyof T[P0] & string>(obj: T, path: `${P0}.${P1}`): T[P0][P1];
>getProp : { <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string, P2 extends keyof T[P0][P1] & string>(obj: T, path: `${P0}.${P1}.${P2}`): T[P0][P1][P2]; <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string>(obj: T, path: `${P0}.${P1}`): T[P0][P1]; <T, P0 extends keyof T & string>(obj: T, path: P0): T[P0]; (obj: object, path: string): unknown; }
>obj : T
>path : `${P0}.${P1}`
declare function getProp<T, P0 extends keyof T & string>(obj: T, path: P0): T[P0];
>getProp : { <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string, P2 extends keyof T[P0][P1] & string>(obj: T, path: `${P0}.${P1}.${P2}`): T[P0][P1][P2]; <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string>(obj: T, path: `${P0}.${P1}`): T[P0][P1]; <T, P0 extends keyof T & string>(obj: T, path: P0): T[P0]; (obj: object, path: string): unknown; }
>obj : T
>path : P0
declare function getProp(obj: object, path: string): unknown;
>getProp : { <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string, P2 extends keyof T[P0][P1] & string>(obj: T, path: `${P0}.${P1}.${P2}`): T[P0][P1][P2]; <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string>(obj: T, path: `${P0}.${P1}`): T[P0][P1]; <T, P0 extends keyof T & string>(obj: T, path: P0): T[P0]; (obj: object, path: string): unknown; }
>obj : object
>path : string
let p1 = getProp({ a: { b: {c: 42, d: 'hello' }}} as const, 'a');
>p1 : { readonly b: { readonly c: 42; readonly d: "hello"; }; }
>getProp({ a: { b: {c: 42, d: 'hello' }}} as const, 'a') : { readonly b: { readonly c: 42; readonly d: "hello"; }; }
>getProp : { <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string, P2 extends keyof T[P0][P1] & string>(obj: T, path: `${P0}.${P1}.${P2}`): T[P0][P1][P2]; <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string>(obj: T, path: `${P0}.${P1}`): T[P0][P1]; <T, P0 extends keyof T & string>(obj: T, path: P0): T[P0]; (obj: object, path: string): unknown; }
>{ a: { b: {c: 42, d: 'hello' }}} as const : { readonly a: { readonly b: { readonly c: 42; readonly d: "hello"; }; }; }
>{ a: { b: {c: 42, d: 'hello' }}} : { readonly a: { readonly b: { readonly c: 42; readonly d: "hello"; }; }; }
>a : { readonly b: { readonly c: 42; readonly d: "hello"; }; }
>{ b: {c: 42, d: 'hello' }} : { readonly b: { readonly c: 42; readonly d: "hello"; }; }
>b : { readonly c: 42; readonly d: "hello"; }
>{c: 42, d: 'hello' } : { readonly c: 42; readonly d: "hello"; }
>c : 42
>42 : 42
>d : "hello"
>'hello' : "hello"
>'a' : "a"
let p2 = getProp({ a: { b: {c: 42, d: 'hello' }}} as const, 'a.b');
>p2 : { readonly c: 42; readonly d: "hello"; }
>getProp({ a: { b: {c: 42, d: 'hello' }}} as const, 'a.b') : { readonly c: 42; readonly d: "hello"; }
>getProp : { <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string, P2 extends keyof T[P0][P1] & string>(obj: T, path: `${P0}.${P1}.${P2}`): T[P0][P1][P2]; <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string>(obj: T, path: `${P0}.${P1}`): T[P0][P1]; <T, P0 extends keyof T & string>(obj: T, path: P0): T[P0]; (obj: object, path: string): unknown; }
>{ a: { b: {c: 42, d: 'hello' }}} as const : { readonly a: { readonly b: { readonly c: 42; readonly d: "hello"; }; }; }
>{ a: { b: {c: 42, d: 'hello' }}} : { readonly a: { readonly b: { readonly c: 42; readonly d: "hello"; }; }; }
>a : { readonly b: { readonly c: 42; readonly d: "hello"; }; }
>{ b: {c: 42, d: 'hello' }} : { readonly b: { readonly c: 42; readonly d: "hello"; }; }
>b : { readonly c: 42; readonly d: "hello"; }
>{c: 42, d: 'hello' } : { readonly c: 42; readonly d: "hello"; }
>c : 42
>42 : 42
>d : "hello"
>'hello' : "hello"
>'a.b' : "a.b"
let p3 = getProp({ a: { b: {c: 42, d: 'hello' }}} as const, 'a.b.d');
>p3 : "hello"
>getProp({ a: { b: {c: 42, d: 'hello' }}} as const, 'a.b.d') : "hello"
>getProp : { <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string, P2 extends keyof T[P0][P1] & string>(obj: T, path: `${P0}.${P1}.${P2}`): T[P0][P1][P2]; <T, P0 extends keyof T & string, P1 extends keyof T[P0] & string>(obj: T, path: `${P0}.${P1}`): T[P0][P1]; <T, P0 extends keyof T & string>(obj: T, path: P0): T[P0]; (obj: object, path: string): unknown; }
>{ a: { b: {c: 42, d: 'hello' }}} as const : { readonly a: { readonly b: { readonly c: 42; readonly d: "hello"; }; }; }
>{ a: { b: {c: 42, d: 'hello' }}} : { readonly a: { readonly b: { readonly c: 42; readonly d: "hello"; }; }; }
>a : { readonly b: { readonly c: 42; readonly d: "hello"; }; }
>{ b: {c: 42, d: 'hello' }} : { readonly b: { readonly c: 42; readonly d: "hello"; }; }
>b : { readonly c: 42; readonly d: "hello"; }
>{c: 42, d: 'hello' } : { readonly c: 42; readonly d: "hello"; }
>c : 42
>42 : 42
>d : "hello"
>'hello' : "hello"
>'a.b.d' : "a.b.d"
type PropType<T, Path extends string> =
>PropType : PropType<T, Path>
string extends Path ? unknown :
Path extends keyof T ? T[Path] :
Path extends `${infer K}.${infer R}` ? K extends keyof T ? PropType<T[K], R> : unknown :
unknown;
declare function getPropValue<T, P extends string>(obj: T, path: P): PropType<T, P>;
>getPropValue : <T, P extends string>(obj: T, path: P) => PropType<T, P>
>obj : T
>path : P
declare const s: string;
>s : string
const obj = { a: { b: {c: 42, d: 'hello' }}};
>obj : { a: { b: { c: number; d: string; }; }; }
>{ a: { b: {c: 42, d: 'hello' }}} : { a: { b: { c: number; d: string; }; }; }
>a : { b: { c: number; d: string; }; }
>{ b: {c: 42, d: 'hello' }} : { b: { c: number; d: string; }; }
>b : { c: number; d: string; }
>{c: 42, d: 'hello' } : { c: number; d: string; }
>c : number
>42 : 42
>d : string
>'hello' : "hello"
getPropValue(obj, 'a'); // { b: {c: number, d: string } }
>getPropValue(obj, 'a') : { b: { c: number; d: string; }; }
>getPropValue : <T, P extends string>(obj: T, path: P) => PropType<T, P>
>obj : { a: { b: { c: number; d: string; }; }; }
>'a' : "a"
getPropValue(obj, 'a.b'); // {c: number, d: string }
>getPropValue(obj, 'a.b') : { c: number; d: string; }
>getPropValue : <T, P extends string>(obj: T, path: P) => PropType<T, P>
>obj : { a: { b: { c: number; d: string; }; }; }
>'a.b' : "a.b"
getPropValue(obj, 'a.b.d'); // string
>getPropValue(obj, 'a.b.d') : string
>getPropValue : <T, P extends string>(obj: T, path: P) => PropType<T, P>
>obj : { a: { b: { c: number; d: string; }; }; }
>'a.b.d' : "a.b.d"
getPropValue(obj, 'a.b.x'); // unknown
>getPropValue(obj, 'a.b.x') : unknown
>getPropValue : <T, P extends string>(obj: T, path: P) => PropType<T, P>
>obj : { a: { b: { c: number; d: string; }; }; }
>'a.b.x' : "a.b.x"
getPropValue(obj, s); // unknown
>getPropValue(obj, s) : unknown
>getPropValue : <T, P extends string>(obj: T, path: P) => PropType<T, P>
>obj : { a: { b: { c: number; d: string; }; }; }
>s : string
// Infer type variables in template literals have string constraint
type S1<T> = T extends `foo${infer U}bar` ? S2<U> : never;
>S1 : S1<T>
type S2<S extends string> = S;
>S2 : S
// Check that infer T declarations are validated
type TV1 = `${infer X}`;
>TV1 : `${X}`
// Batched single character inferences for lower recursion depth
type Chars<S extends string> =
>Chars : Chars<S>
string extends S ? string[] :
S extends `${infer C0}${infer C1}${infer C2}${infer C3}${infer C4}${infer C5}${infer C6}${infer C7}${infer C8}${infer C9}${infer R}` ? [C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, ...Chars<R>] :
S extends `${infer C}${infer R}` ? [C, ...Chars<R>] :
S extends '' ? [] :
never;
type L1 = Chars<'FooBarBazThisIsALongerString'>; // ['F', 'o', 'o', 'B', 'a', 'r', ...]
>L1 : ["F", "o", "o", "B", "a", "r", "B", "a", "z", "T", "h", "i", "s", "I", "s", "A", "L", "o", "n", "g", "e", "r", "S", "t", "r", "i", "n", "g"]
// Infer never when source isn't a literal type that matches the pattern
type Foo<T> = T extends `*${infer S}*` ? S : never;
>Foo : Foo<T>
type TF1 = Foo<any>; // never
>TF1 : never
type TF2 = Foo<string>; // never
>TF2 : never
type TF3 = Foo<'abc'>; // never
>TF3 : never
type TF4 = Foo<'*abc*'>; // 'abc'
>TF4 : "abc"
// Cross product unions limited to 100,000 constituents
type A = any;
>A : any
type U1 = {a1:A} | {b1:A} | {c1:A} | {d1:A} | {e1:A} | {f1:A} | {g1:A} | {h1:A} | {i1:A} | {j1:A};
>U1 : U1
>a1 : any
>b1 : any
>c1 : any
>d1 : any
>e1 : any
>f1 : any
>g1 : any
>h1 : any
>i1 : any
>j1 : any
type U2 = {a2:A} | {b2:A} | {c2:A} | {d2:A} | {e2:A} | {f2:A} | {g2:A} | {h2:A} | {i2:A} | {j2:A};
>U2 : U2
>a2 : any
>b2 : any
>c2 : any
>d2 : any
>e2 : any
>f2 : any
>g2 : any
>h2 : any
>i2 : any
>j2 : any
type U3 = {a3:A} | {b3:A} | {c3:A} | {d3:A} | {e3:A} | {f3:A} | {g3:A} | {h3:A} | {i3:A} | {j3:A};
>U3 : U3
>a3 : any
>b3 : any
>c3 : any
>d3 : any
>e3 : any
>f3 : any
>g3 : any
>h3 : any
>i3 : any
>j3 : any
type U4 = {a4:A} | {b4:A} | {c4:A} | {d4:A} | {e4:A} | {f4:A} | {g4:A} | {h4:A} | {i4:A} | {j4:A};
>U4 : U4
>a4 : any
>b4 : any
>c4 : any
>d4 : any
>e4 : any
>f4 : any
>g4 : any
>h4 : any
>i4 : any
>j4 : any
type U5 = {a5:A} | {b5:A} | {c5:A} | {d5:A} | {e5:A} | {f5:A} | {g5:A} | {h5:A} | {i5:A} | {j5:A};
>U5 : U5
>a5 : any
>b5 : any
>c5 : any
>d5 : any
>e5 : any
>f5 : any
>g5 : any
>h5 : any
>i5 : any
>j5 : any
type U100000 = U1 & U2 & U3 & U4 & U5; // Error
>U100000 : any
type Digits = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
>Digits : Digits
type D100000 = `${Digits}${Digits}${Digits}${Digits}${Digits}`; // Error
>D100000 : any
type TDigits = [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9];
>TDigits : TDigits
type T100000 = [...TDigits, ...TDigits, ...TDigits, ...TDigits, ...TDigits]; // Error
>T100000 : any
// Repro from #40863
type IsNegative<T extends number> = `${T}` extends `-${string}` ? true : false;
>IsNegative : IsNegative<T>
>true : true
>false : false
type AA<T extends number, Q extends number> =
>AA : AA<T, Q>
[true, true] extends [IsNegative<T>, IsNegative<Q>] ? 'Every thing is ok!' : ['strange', IsNegative<T>, IsNegative<Q>];
>true : true
>true : true
type BB = AA<-2, -2>;
>BB : "Every thing is ok!"
>-2 : -2
>2 : 2
>-2 : -2
>2 : 2
// Repro from #40970
type PathKeys<T> =
>PathKeys : PathKeys<T>
T extends readonly any[] ? Extract<keyof T, `${number}`> | SubKeys<T, Extract<keyof T, `${number}`>> :
T extends object ? Extract<keyof T, string> | SubKeys<T, Extract<keyof T, string>> :
never;
type SubKeys<T, K extends string> = K extends keyof T ? `${K}.${PathKeys<T[K]>}` : never;
>SubKeys : SubKeys<T, K>
declare function getProp2<T, P extends PathKeys<T>>(obj: T, path: P): PropType<T, P>;
>getProp2 : <T, P extends PathKeys<T>>(obj: T, path: P) => PropType<T, P>
>obj : T
>path : P
const obj2 = {
>obj2 : { readonly name: "John"; readonly age: 42; readonly cars: readonly [{ readonly make: "Ford"; readonly age: 10; }, { readonly make: "Trabant"; readonly age: 35; }]; }
>{ name: 'John', age: 42, cars: [ { make: 'Ford', age: 10 }, { make: 'Trabant', age: 35 } ]} as const : { readonly name: "John"; readonly age: 42; readonly cars: readonly [{ readonly make: "Ford"; readonly age: 10; }, { readonly make: "Trabant"; readonly age: 35; }]; }
>{ name: 'John', age: 42, cars: [ { make: 'Ford', age: 10 }, { make: 'Trabant', age: 35 } ]} : { readonly name: "John"; readonly age: 42; readonly cars: readonly [{ readonly make: "Ford"; readonly age: 10; }, { readonly make: "Trabant"; readonly age: 35; }]; }
name: 'John',
>name : "John"
>'John' : "John"
age: 42,
>age : 42
>42 : 42
cars: [
>cars : readonly [{ readonly make: "Ford"; readonly age: 10; }, { readonly make: "Trabant"; readonly age: 35; }]
>[ { make: 'Ford', age: 10 }, { make: 'Trabant', age: 35 } ] : readonly [{ readonly make: "Ford"; readonly age: 10; }, { readonly make: "Trabant"; readonly age: 35; }]
{ make: 'Ford', age: 10 },
>{ make: 'Ford', age: 10 } : { readonly make: "Ford"; readonly age: 10; }
>make : "Ford"
>'Ford' : "Ford"
>age : 10
>10 : 10
{ make: 'Trabant', age: 35 }
>{ make: 'Trabant', age: 35 } : { readonly make: "Trabant"; readonly age: 35; }
>make : "Trabant"
>'Trabant' : "Trabant"
>age : 35
>35 : 35
]
} as const;
let make = getProp2(obj2, 'cars.1.make'); // 'Trabant'
>make : "Trabant"
>getProp2(obj2, 'cars.1.make') : "Trabant"
>getProp2 : <T, P extends PathKeys<T>>(obj: T, path: P) => PropType<T, P>
>obj2 : { readonly name: "John"; readonly age: 42; readonly cars: readonly [{ readonly make: "Ford"; readonly age: 10; }, { readonly make: "Trabant"; readonly age: 35; }]; }
>'cars.1.make' : "cars.1.make"