-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathliteralTypeWidening.types
480 lines (381 loc) · 10.7 KB
/
literalTypeWidening.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
=== tests/cases/conformance/types/literal/literalTypeWidening.ts ===
// Widening vs. non-widening literal types
function f1() {
>f1 : () => void
const c1 = "hello"; // Widening type "hello"
>c1 : "hello"
>"hello" : "hello"
let v1 = c1; // Type string
>v1 : string
>c1 : "hello"
const c2 = c1; // Widening type "hello"
>c2 : "hello"
>c1 : "hello"
let v2 = c2; // Type string
>v2 : string
>c2 : "hello"
const c3: "hello" = "hello"; // Type "hello"
>c3 : "hello"
>"hello" : "hello"
let v3 = c3; // Type "hello"
>v3 : "hello"
>c3 : "hello"
const c4: "hello" = c1; // Type "hello"
>c4 : "hello"
>c1 : "hello"
let v4 = c4; // Type "hello"
>v4 : "hello"
>c4 : "hello"
}
function f2(cond: boolean) {
>f2 : (cond: boolean) => void
>cond : boolean
const c1 = cond ? "foo" : "bar"; // widening "foo" | widening "bar"
>c1 : "foo" | "bar"
>cond ? "foo" : "bar" : "foo" | "bar"
>cond : boolean
>"foo" : "foo"
>"bar" : "bar"
const c2: "foo" | "bar" = c1; // "foo" | "bar"
>c2 : "foo" | "bar"
>c1 : "foo" | "bar"
const c3 = cond ? c1 : c2; // "foo" | "bar"
>c3 : "foo" | "bar"
>cond ? c1 : c2 : "foo" | "bar"
>cond : boolean
>c1 : "foo" | "bar"
>c2 : "foo" | "bar"
const c4 = cond ? c3 : "baz"; // "foo" | "bar" | widening "baz"
>c4 : "foo" | "bar" | "baz"
>cond ? c3 : "baz" : "foo" | "bar" | "baz"
>cond : boolean
>c3 : "foo" | "bar"
>"baz" : "baz"
const c5: "foo" | "bar" | "baz" = c4; // "foo" | "bar" | "baz"
>c5 : "foo" | "bar" | "baz"
>c4 : "foo" | "bar" | "baz"
let v1 = c1; // string
>v1 : string
>c1 : "foo" | "bar"
let v2 = c2; // "foo" | "bar"
>v2 : "foo" | "bar"
>c2 : "foo" | "bar"
let v3 = c3; // "foo" | "bar"
>v3 : "foo" | "bar"
>c3 : "foo" | "bar"
let v4 = c4; // string
>v4 : string
>c4 : "foo" | "bar" | "baz"
let v5 = c5; // "foo" | "bar" | "baz"
>v5 : "foo" | "bar" | "baz"
>c5 : "foo" | "bar" | "baz"
}
function f3() {
>f3 : () => void
const c1 = 123; // Widening type 123
>c1 : 123
>123 : 123
let v1 = c1; // Type number
>v1 : number
>c1 : 123
const c2 = c1; // Widening type 123
>c2 : 123
>c1 : 123
let v2 = c2; // Type number
>v2 : number
>c2 : 123
const c3: 123 = 123; // Type 123
>c3 : 123
>123 : 123
let v3 = c3; // Type 123
>v3 : 123
>c3 : 123
const c4: 123 = c1; // Type 123
>c4 : 123
>c1 : 123
let v4 = c4; // Type 123
>v4 : 123
>c4 : 123
}
function f4(cond: boolean) {
>f4 : (cond: boolean) => void
>cond : boolean
const c1 = cond ? 123 : 456; // widening 123 | widening 456
>c1 : 123 | 456
>cond ? 123 : 456 : 123 | 456
>cond : boolean
>123 : 123
>456 : 456
const c2: 123 | 456 = c1; // 123 | 456
>c2 : 123 | 456
>c1 : 123 | 456
const c3 = cond ? c1 : c2; // 123 | 456
>c3 : 123 | 456
>cond ? c1 : c2 : 123 | 456
>cond : boolean
>c1 : 123 | 456
>c2 : 123 | 456
const c4 = cond ? c3 : 789; // 123 | 456 | widening 789
>c4 : 123 | 456 | 789
>cond ? c3 : 789 : 123 | 456 | 789
>cond : boolean
>c3 : 123 | 456
>789 : 789
const c5: 123 | 456 | 789 = c4; // 123 | 456 | 789
>c5 : 123 | 456 | 789
>c4 : 123 | 456 | 789
let v1 = c1; // number
>v1 : number
>c1 : 123 | 456
let v2 = c2; // 123 | 456
>v2 : 123 | 456
>c2 : 123 | 456
let v3 = c3; // 123 | 456
>v3 : 123 | 456
>c3 : 123 | 456
let v4 = c4; // number
>v4 : number
>c4 : 123 | 456 | 789
let v5 = c5; // 123 | 456 | 789
>v5 : 123 | 456 | 789
>c5 : 123 | 456 | 789
}
function f5() {
>f5 : () => void
const c1 = "foo";
>c1 : "foo"
>"foo" : "foo"
let v1 = c1;
>v1 : string
>c1 : "foo"
const c2: "foo" = "foo";
>c2 : "foo"
>"foo" : "foo"
let v2 = c2;
>v2 : "foo"
>c2 : "foo"
const c3 = "foo" as "foo";
>c3 : "foo"
>"foo" as "foo" : "foo"
>"foo" : "foo"
let v3 = c3;
>v3 : "foo"
>c3 : "foo"
const c4 = <"foo">"foo";
>c4 : "foo"
><"foo">"foo" : "foo"
>"foo" : "foo"
let v4 = c4;
>v4 : "foo"
>c4 : "foo"
}
declare function widening<T>(x: T): T;
>widening : <T>(x: T) => T
>x : T
declare function nonWidening<T extends string | number | symbol>(x: T): T;
>nonWidening : <T extends string | number | symbol>(x: T) => T
>x : T
function f6(cond: boolean) {
>f6 : (cond: boolean) => void
>cond : boolean
let x1 = widening('a');
>x1 : string
>widening('a') : "a"
>widening : <T>(x: T) => T
>'a' : "a"
let x2 = widening(10);
>x2 : number
>widening(10) : 10
>widening : <T>(x: T) => T
>10 : 10
let x3 = widening(cond ? 'a' : 10);
>x3 : string | number
>widening(cond ? 'a' : 10) : "a" | 10
>widening : <T>(x: T) => T
>cond ? 'a' : 10 : "a" | 10
>cond : boolean
>'a' : "a"
>10 : 10
let y1 = nonWidening('a');
>y1 : "a"
>nonWidening('a') : "a"
>nonWidening : <T extends string | number | symbol>(x: T) => T
>'a' : "a"
let y2 = nonWidening(10);
>y2 : 10
>nonWidening(10) : 10
>nonWidening : <T extends string | number | symbol>(x: T) => T
>10 : 10
let y3 = nonWidening(cond ? 'a' : 10);
>y3 : "a" | 10
>nonWidening(cond ? 'a' : 10) : "a" | 10
>nonWidening : <T extends string | number | symbol>(x: T) => T
>cond ? 'a' : 10 : "a" | 10
>cond : boolean
>'a' : "a"
>10 : 10
}
// Repro from #10898
type FAILURE = "FAILURE";
>FAILURE : "FAILURE"
const FAILURE = "FAILURE";
>FAILURE : "FAILURE"
>"FAILURE" : "FAILURE"
type Result<T> = T | FAILURE;
>Result : Result<T>
function doWork<T>(): Result<T> {
>doWork : <T>() => Result<T>
return FAILURE;
>FAILURE : "FAILURE"
}
function isSuccess<T>(result: Result<T>): result is T {
>isSuccess : <T>(result: Result<T>) => result is T
>result : Result<T>
return !isFailure(result);
>!isFailure(result) : boolean
>isFailure(result) : boolean
>isFailure : <T>(result: Result<T>) => result is "FAILURE"
>result : Result<T>
}
function isFailure<T>(result: Result<T>): result is FAILURE {
>isFailure : <T>(result: Result<T>) => result is "FAILURE"
>result : Result<T>
return result === FAILURE;
>result === FAILURE : boolean
>result : Result<T>
>FAILURE : "FAILURE"
}
function increment(x: number): number {
>increment : (x: number) => number
>x : number
return x + 1;
>x + 1 : number
>x : number
>1 : 1
}
let result = doWork<number>();
>result : Result<number>
>doWork<number>() : Result<number>
>doWork : <T>() => Result<T>
if (isSuccess(result)) {
>isSuccess(result) : boolean
>isSuccess : <T>(result: Result<T>) => result is T
>result : Result<number>
increment(result);
>increment(result) : number
>increment : (x: number) => number
>result : number
}
// Repro from #10898
type TestEvent = "onmouseover" | "onmouseout";
>TestEvent : TestEvent
function onMouseOver(): TestEvent { return "onmouseover"; }
>onMouseOver : () => TestEvent
>"onmouseover" : "onmouseover"
let x = onMouseOver();
>x : TestEvent
>onMouseOver() : TestEvent
>onMouseOver : () => TestEvent
// Repro from #23649
export function Set<K extends string>(...keys: K[]): Record<K, true | undefined> {
>Set : <K extends string>(...keys: K[]) => Record<K, true>
>keys : K[]
>true : true
const result = {} as Record<K, true | undefined>
>result : Record<K, true>
>{} as Record<K, true | undefined> : Record<K, true>
>{} : {}
>true : true
keys.forEach(key => result[key] = true)
>keys.forEach(key => result[key] = true) : void
>keys.forEach : (callbackfn: (value: K, index: number, array: K[]) => void, thisArg?: any) => void
>keys : K[]
>forEach : (callbackfn: (value: K, index: number, array: K[]) => void, thisArg?: any) => void
>key => result[key] = true : (key: K) => boolean
>key : K
>result[key] = true : true
>result[key] : Record<K, true>[K]
>result : Record<K, true>
>key : K
>true : true
return result
>result : Record<K, true>
}
export function keys<K extends string, V>(obj: Record<K, V>): K[] {
>keys : <K extends string, V>(obj: Record<K, V>) => K[]
>obj : Record<K, V>
return Object.keys(obj) as K[]
>Object.keys(obj) as K[] : K[]
>Object.keys(obj) : string[]
>Object.keys : (o: object) => string[]
>Object : ObjectConstructor
>keys : (o: object) => string[]
>obj : Record<K, V>
}
type Obj = { code: LangCode }
>Obj : Obj
>code : "fr" | "en" | "es" | "it" | "nl"
const langCodeSet = Set('fr', 'en', 'es', 'it', 'nl')
>langCodeSet : Record<"fr" | "en" | "es" | "it" | "nl", true>
>Set('fr', 'en', 'es', 'it', 'nl') : Record<"fr" | "en" | "es" | "it" | "nl", true>
>Set : <K extends string>(...keys: K[]) => Record<K, true>
>'fr' : "fr"
>'en' : "en"
>'es' : "es"
>'it' : "it"
>'nl' : "nl"
export type LangCode = keyof typeof langCodeSet
>LangCode : "fr" | "en" | "es" | "it" | "nl"
>langCodeSet : Record<"fr" | "en" | "es" | "it" | "nl", true>
export const langCodes = keys(langCodeSet)
>langCodes : ("fr" | "en" | "es" | "it" | "nl")[]
>keys(langCodeSet) : ("fr" | "en" | "es" | "it" | "nl")[]
>keys : <K extends string, V>(obj: Record<K, V>) => K[]
>langCodeSet : Record<"fr" | "en" | "es" | "it" | "nl", true>
const arr: Obj[] = langCodes.map(code => ({ code }))
>arr : Obj[]
>langCodes.map(code => ({ code })) : { code: "fr" | "en" | "es" | "it" | "nl"; }[]
>langCodes.map : <U>(callbackfn: (value: "fr" | "en" | "es" | "it" | "nl", index: number, array: ("fr" | "en" | "es" | "it" | "nl")[]) => U, thisArg?: any) => U[]
>langCodes : ("fr" | "en" | "es" | "it" | "nl")[]
>map : <U>(callbackfn: (value: "fr" | "en" | "es" | "it" | "nl", index: number, array: ("fr" | "en" | "es" | "it" | "nl")[]) => U, thisArg?: any) => U[]
>code => ({ code }) : (code: "fr" | "en" | "es" | "it" | "nl") => { code: "fr" | "en" | "es" | "it" | "nl"; }
>code : "fr" | "en" | "es" | "it" | "nl"
>({ code }) : { code: "fr" | "en" | "es" | "it" | "nl"; }
>{ code } : { code: "fr" | "en" | "es" | "it" | "nl"; }
>code : "fr" | "en" | "es" | "it" | "nl"
// Repro from #29081
function test<T extends { a: string, b: string }>(obj: T): T {
>test : <T extends { a: string; b: string; }>(obj: T) => T
>a : string
>b : string
>obj : T
let { a, ...rest } = obj;
>a : string
>rest : Pick<T, Exclude<keyof T, "a">>
>obj : T
return { a: 'hello', ...rest } as T;
>{ a: 'hello', ...rest } as T : T
>{ a: 'hello', ...rest } : { a: string; } & Pick<T, Exclude<keyof T, "a">>
>a : string
>'hello' : "hello"
>rest : Pick<T, Exclude<keyof T, "a">>
}
// Repro from #32169
declare function f<T>(x: T): NonNullable<T>;
>f : <T>(x: T) => NonNullable<T>
>x : T
enum E { A, B }
>E : E
>A : E.A
>B : E.B
const a = f(E.A);
>a : E.A
>f(E.A) : E.A
>f : <T>(x: T) => NonNullable<T>
>E.A : E.A
>E : typeof E
>A : E.A
const b: E.A = a;
>b : E.A
>E : any
>a : E.A