forked from microsoft/TypeScript
/
correlatedUnions.types
697 lines (565 loc) · 21.3 KB
/
correlatedUnions.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
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
=== tests/cases/compiler/correlatedUnions.ts ===
// Various repros from #30581
type RecordMap = { n: number, s: string, b: boolean };
>RecordMap : RecordMap
>n : number
>s : string
>b : boolean
type UnionRecord<K extends keyof RecordMap = keyof RecordMap> = { [P in K]: {
>UnionRecord : UnionRecord<K>
kind: P,
>kind : P
v: RecordMap[P],
>v : RecordMap[P]
f: (v: RecordMap[P]) => void
>f : (v: RecordMap[P]) => void
>v : RecordMap[P]
}}[K];
function processRecord<K extends keyof RecordMap>(rec: UnionRecord<K>) {
>processRecord : <K extends keyof RecordMap>(rec: UnionRecord<K>) => void
>rec : UnionRecord<K>
rec.f(rec.v);
>rec.f(rec.v) : void
>rec.f : (v: RecordMap[K]) => void
>rec : UnionRecord<K>
>f : (v: RecordMap[K]) => void
>rec.v : RecordMap[K]
>rec : UnionRecord<K>
>v : RecordMap[K]
}
declare const r1: UnionRecord<'n'>; // { kind: 'n', v: number, f: (v: number) => void }
>r1 : { kind: "n"; v: number; f: (v: number) => void; }
declare const r2: UnionRecord; // { kind: 'n', ... } | { kind: 's', ... } | { kind: 'b', ... }
>r2 : UnionRecord<keyof RecordMap>
processRecord(r1);
>processRecord(r1) : void
>processRecord : <K extends keyof RecordMap>(rec: UnionRecord<K>) => void
>r1 : { kind: "n"; v: number; f: (v: number) => void; }
processRecord(r2);
>processRecord(r2) : void
>processRecord : <K extends keyof RecordMap>(rec: UnionRecord<K>) => void
>r2 : UnionRecord<keyof RecordMap>
processRecord({ kind: 'n', v: 42, f: v => v.toExponential() });
>processRecord({ kind: 'n', v: 42, f: v => v.toExponential() }) : void
>processRecord : <K extends keyof RecordMap>(rec: UnionRecord<K>) => void
>{ kind: 'n', v: 42, f: v => v.toExponential() } : { kind: "n"; v: number; f: (v: number) => string; }
>kind : "n"
>'n' : "n"
>v : number
>42 : 42
>f : (v: number) => string
>v => v.toExponential() : (v: number) => string
>v : number
>v.toExponential() : string
>v.toExponential : (fractionDigits?: number | undefined) => string
>v : number
>toExponential : (fractionDigits?: number | undefined) => string
// --------
type TextFieldData = { value: string }
>TextFieldData : TextFieldData
>value : string
type SelectFieldData = { options: string[], selectedValue: string }
>SelectFieldData : SelectFieldData
>options : string[]
>selectedValue : string
type FieldMap = {
>FieldMap : FieldMap
text: TextFieldData;
>text : TextFieldData
select: SelectFieldData;
>select : SelectFieldData
}
type FormField<K extends keyof FieldMap> = { type: K, data: FieldMap[K] };
>FormField : FormField<K>
>type : K
>data : FieldMap[K]
type RenderFunc<K extends keyof FieldMap> = (props: FieldMap[K]) => void;
>RenderFunc : RenderFunc<K>
>props : FieldMap[K]
type RenderFuncMap = { [K in keyof FieldMap]: RenderFunc<K> };
>RenderFuncMap : RenderFuncMap
function renderTextField(props: TextFieldData) {}
>renderTextField : (props: TextFieldData) => void
>props : TextFieldData
function renderSelectField(props: SelectFieldData) {}
>renderSelectField : (props: SelectFieldData) => void
>props : SelectFieldData
const renderFuncs: RenderFuncMap = {
>renderFuncs : RenderFuncMap
>{ text: renderTextField, select: renderSelectField,} : { text: (props: TextFieldData) => void; select: (props: SelectFieldData) => void; }
text: renderTextField,
>text : (props: TextFieldData) => void
>renderTextField : (props: TextFieldData) => void
select: renderSelectField,
>select : (props: SelectFieldData) => void
>renderSelectField : (props: SelectFieldData) => void
};
function renderField<K extends keyof FieldMap>(field: FormField<K>) {
>renderField : <K extends keyof FieldMap>(field: FormField<K>) => void
>field : FormField<K>
const renderFn = renderFuncs[field.type];
>renderFn : RenderFuncMap[K]
>renderFuncs[field.type] : RenderFuncMap[K]
>renderFuncs : RenderFuncMap
>field.type : K
>field : FormField<K>
>type : K
renderFn(field.data);
>renderFn(field.data) : void
>renderFn : RenderFuncMap[K]
>field.data : FieldMap[K]
>field : FormField<K>
>data : FieldMap[K]
}
// --------
type TypeMap = {
>TypeMap : TypeMap
foo: string,
>foo : string
bar: number
>bar : number
};
type Keys = keyof TypeMap;
>Keys : keyof TypeMap
type HandlerMap = { [P in Keys]: (x: TypeMap[P]) => void };
>HandlerMap : HandlerMap
>x : TypeMap[P]
const handlers: HandlerMap = {
>handlers : HandlerMap
>{ foo: s => s.length, bar: n => n.toFixed(2)} : { foo: (s: string) => number; bar: (n: number) => string; }
foo: s => s.length,
>foo : (s: string) => number
>s => s.length : (s: string) => number
>s : string
>s.length : number
>s : string
>length : number
bar: n => n.toFixed(2)
>bar : (n: number) => string
>n => n.toFixed(2) : (n: number) => string
>n : number
>n.toFixed(2) : string
>n.toFixed : (fractionDigits?: number | undefined) => string
>n : number
>toFixed : (fractionDigits?: number | undefined) => string
>2 : 2
};
type DataEntry<K extends Keys = Keys> = { [P in K]: {
>DataEntry : DataEntry<K>
type: P,
>type : P
data: TypeMap[P]
>data : TypeMap[P]
}}[K];
const data: DataEntry[] = [
>data : DataEntry<keyof TypeMap>[]
>[ { type: 'foo', data: 'abc' }, { type: 'foo', data: 'def' }, { type: 'bar', data: 42 },] : ({ type: "foo"; data: string; } | { type: "bar"; data: number; })[]
{ type: 'foo', data: 'abc' },
>{ type: 'foo', data: 'abc' } : { type: "foo"; data: string; }
>type : "foo"
>'foo' : "foo"
>data : string
>'abc' : "abc"
{ type: 'foo', data: 'def' },
>{ type: 'foo', data: 'def' } : { type: "foo"; data: string; }
>type : "foo"
>'foo' : "foo"
>data : string
>'def' : "def"
{ type: 'bar', data: 42 },
>{ type: 'bar', data: 42 } : { type: "bar"; data: number; }
>type : "bar"
>'bar' : "bar"
>data : number
>42 : 42
];
function process<K extends Keys>(data: DataEntry<K>[]) {
>process : <K extends keyof TypeMap>(data: DataEntry<K>[]) => void
>data : DataEntry<K>[]
data.forEach(block => {
>data.forEach(block => { if (block.type in handlers) { handlers[block.type](block.data) } }) : void
>data.forEach : (callbackfn: (value: DataEntry<K>, index: number, array: DataEntry<K>[]) => void, thisArg?: any) => void
>data : DataEntry<K>[]
>forEach : (callbackfn: (value: DataEntry<K>, index: number, array: DataEntry<K>[]) => void, thisArg?: any) => void
>block => { if (block.type in handlers) { handlers[block.type](block.data) } } : (block: DataEntry<K>) => void
>block : DataEntry<K>
if (block.type in handlers) {
>block.type in handlers : boolean
>block.type : K
>block : DataEntry<K>
>type : K
>handlers : HandlerMap
handlers[block.type](block.data)
>handlers[block.type](block.data) : void
>handlers[block.type] : HandlerMap[K]
>handlers : HandlerMap
>block.type : K
>block : DataEntry<K>
>type : K
>block.data : TypeMap[K]
>block : DataEntry<K>
>data : TypeMap[K]
}
});
}
process(data);
>process(data) : void
>process : <K extends keyof TypeMap>(data: DataEntry<K>[]) => void
>data : DataEntry<keyof TypeMap>[]
process([{ type: 'foo', data: 'abc' }]);
>process([{ type: 'foo', data: 'abc' }]) : void
>process : <K extends keyof TypeMap>(data: DataEntry<K>[]) => void
>[{ type: 'foo', data: 'abc' }] : { type: "foo"; data: string; }[]
>{ type: 'foo', data: 'abc' } : { type: "foo"; data: string; }
>type : "foo"
>'foo' : "foo"
>data : string
>'abc' : "abc"
// --------
type LetterMap = { A: string, B: number }
>LetterMap : LetterMap
>A : string
>B : number
type LetterCaller<K extends keyof LetterMap> = { [P in K]: { letter: Record<P, LetterMap[P]>, caller: (x: Record<P, LetterMap[P]>) => void } }[K];
>LetterCaller : LetterCaller<K>
>letter : Record<P, LetterMap[P]>
>caller : (x: Record<P, LetterMap[P]>) => void
>x : Record<P, LetterMap[P]>
function call<K extends keyof LetterMap>({ letter, caller }: LetterCaller<K>): void {
>call : <K extends keyof LetterMap>({ letter, caller }: LetterCaller<K>) => void
>letter : Record<K, LetterMap[K]>
>caller : (x: Record<K, LetterMap[K]>) => void
caller(letter);
>caller(letter) : void
>caller : (x: Record<K, LetterMap[K]>) => void
>letter : Record<K, LetterMap[K]>
}
type A = { A: string };
>A : A
>A : string
type B = { B: number };
>B : B
>B : number
type ACaller = (a: A) => void;
>ACaller : ACaller
>a : A
type BCaller = (b: B) => void;
>BCaller : BCaller
>b : B
declare const xx: { letter: A, caller: ACaller } | { letter: B, caller: BCaller };
>xx : { letter: A; caller: ACaller; } | { letter: B; caller: BCaller; }
>letter : A
>caller : ACaller
>letter : B
>caller : BCaller
call(xx);
>call(xx) : void
>call : <K extends keyof LetterMap>({ letter, caller }: LetterCaller<K>) => void
>xx : { letter: A; caller: ACaller; } | { letter: B; caller: BCaller; }
// --------
type Ev<K extends keyof DocumentEventMap> = { [P in K]: {
>Ev : Ev<K>
readonly name: P;
>name : P
readonly once?: boolean;
>once : boolean | undefined
readonly callback: (ev: DocumentEventMap[P]) => void;
>callback : (ev: DocumentEventMap[P]) => void
>ev : DocumentEventMap[P]
}}[K];
function processEvents<K extends keyof DocumentEventMap>(events: Ev<K>[]) {
>processEvents : <K extends keyof DocumentEventMap>(events: Ev<K>[]) => void
>events : Ev<K>[]
for (const event of events) {
>event : Ev<K>
>events : Ev<K>[]
document.addEventListener(event.name, (ev) => event.callback(ev), { once: event.once });
>document.addEventListener(event.name, (ev) => event.callback(ev), { once: event.once }) : void
>document.addEventListener : { <K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions | undefined): void; (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | undefined): void; }
>document : Document
>addEventListener : { <K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions | undefined): void; (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions | undefined): void; }
>event.name : K
>event : Ev<K>
>name : K
>(ev) => event.callback(ev) : (this: Document, ev: DocumentEventMap[K]) => void
>ev : DocumentEventMap[K]
>event.callback(ev) : void
>event.callback : (ev: DocumentEventMap[K]) => void
>event : Ev<K>
>callback : (ev: DocumentEventMap[K]) => void
>ev : DocumentEventMap[K]
>{ once: event.once } : { once: boolean | undefined; }
>once : boolean | undefined
>event.once : boolean | undefined
>event : Ev<K>
>once : boolean | undefined
}
}
function createEventListener<K extends keyof DocumentEventMap>({ name, once = false, callback }: Ev<K>): Ev<K> {
>createEventListener : <K extends keyof DocumentEventMap>({ name, once, callback }: Ev<K>) => Ev<K>
>name : K
>once : boolean
>false : false
>callback : (ev: DocumentEventMap[K]) => void
return { name, once, callback };
>{ name, once, callback } : { name: K; once: boolean; callback: (ev: DocumentEventMap[K]) => void; }
>name : K
>once : boolean
>callback : (ev: DocumentEventMap[K]) => void
}
const clickEvent = createEventListener({
>clickEvent : { readonly name: "click"; readonly once?: boolean | undefined; readonly callback: (ev: MouseEvent) => void; }
>createEventListener({ name: "click", callback: ev => console.log(ev),}) : { readonly name: "click"; readonly once?: boolean | undefined; readonly callback: (ev: MouseEvent) => void; }
>createEventListener : <K extends keyof DocumentEventMap>({ name, once, callback }: Ev<K>) => Ev<K>
>{ name: "click", callback: ev => console.log(ev),} : { name: "click"; callback: (ev: MouseEvent) => void; }
name: "click",
>name : "click"
>"click" : "click"
callback: ev => console.log(ev),
>callback : (ev: MouseEvent) => void
>ev => console.log(ev) : (ev: MouseEvent) => void
>ev : MouseEvent
>console.log(ev) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>ev : MouseEvent
});
const scrollEvent = createEventListener({
>scrollEvent : { readonly name: "scroll"; readonly once?: boolean | undefined; readonly callback: (ev: Event) => void; }
>createEventListener({ name: "scroll", callback: ev => console.log(ev),}) : { readonly name: "scroll"; readonly once?: boolean | undefined; readonly callback: (ev: Event) => void; }
>createEventListener : <K extends keyof DocumentEventMap>({ name, once, callback }: Ev<K>) => Ev<K>
>{ name: "scroll", callback: ev => console.log(ev),} : { name: "scroll"; callback: (ev: Event) => void; }
name: "scroll",
>name : "scroll"
>"scroll" : "scroll"
callback: ev => console.log(ev),
>callback : (ev: Event) => void
>ev => console.log(ev) : (ev: Event) => void
>ev : Event
>console.log(ev) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>ev : Event
});
processEvents([clickEvent, scrollEvent]);
>processEvents([clickEvent, scrollEvent]) : void
>processEvents : <K extends keyof DocumentEventMap>(events: Ev<K>[]) => void
>[clickEvent, scrollEvent] : ({ readonly name: "click"; readonly once?: boolean | undefined; readonly callback: (ev: MouseEvent) => void; } | { readonly name: "scroll"; readonly once?: boolean | undefined; readonly callback: (ev: Event) => void; })[]
>clickEvent : { readonly name: "click"; readonly once?: boolean | undefined; readonly callback: (ev: MouseEvent) => void; }
>scrollEvent : { readonly name: "scroll"; readonly once?: boolean | undefined; readonly callback: (ev: Event) => void; }
processEvents([
>processEvents([ { name: "click", callback: ev => console.log(ev) }, { name: "scroll", callback: ev => console.log(ev) },]) : void
>processEvents : <K extends keyof DocumentEventMap>(events: Ev<K>[]) => void
>[ { name: "click", callback: ev => console.log(ev) }, { name: "scroll", callback: ev => console.log(ev) },] : ({ name: "click"; callback: (ev: MouseEvent) => void; } | { name: "scroll"; callback: (ev: Event) => void; })[]
{ name: "click", callback: ev => console.log(ev) },
>{ name: "click", callback: ev => console.log(ev) } : { name: "click"; callback: (ev: MouseEvent) => void; }
>name : "click"
>"click" : "click"
>callback : (ev: MouseEvent) => void
>ev => console.log(ev) : (ev: MouseEvent) => void
>ev : MouseEvent
>console.log(ev) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>ev : MouseEvent
{ name: "scroll", callback: ev => console.log(ev) },
>{ name: "scroll", callback: ev => console.log(ev) } : { name: "scroll"; callback: (ev: Event) => void; }
>name : "scroll"
>"scroll" : "scroll"
>callback : (ev: Event) => void
>ev => console.log(ev) : (ev: Event) => void
>ev : Event
>console.log(ev) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>ev : Event
]);
// --------
function ff1() {
>ff1 : () => void
type ArgMap = {
>ArgMap : { sum: [a: number, b: number]; concat: [a: string, b: string, c: string]; }
sum: [a: number, b: number],
>sum : [a: number, b: number]
concat: [a: string, b: string, c: string]
>concat : [a: string, b: string, c: string]
}
type Keys = keyof ArgMap;
>Keys : keyof { sum: [a: number, b: number]; concat: [a: string, b: string, c: string]; }
const funs: { [P in Keys]: (...args: ArgMap[P]) => void } = {
>funs : { concat: (a: string, b: string, c: string) => void; sum: (a: number, b: number) => void; }
>args : { sum: [a: number, b: number]; concat: [a: string, b: string, c: string]; }[P]
>{ sum: (a, b) => a + b, concat: (a, b, c) => a + b + c } : { sum: (a: number, b: number) => number; concat: (a: string, b: string, c: string) => string; }
sum: (a, b) => a + b,
>sum : (a: number, b: number) => number
>(a, b) => a + b : (a: number, b: number) => number
>a : number
>b : number
>a + b : number
>a : number
>b : number
concat: (a, b, c) => a + b + c
>concat : (a: string, b: string, c: string) => string
>(a, b, c) => a + b + c : (a: string, b: string, c: string) => string
>a : string
>b : string
>c : string
>a + b + c : string
>a + b : string
>a : string
>b : string
>c : string
}
function apply<K extends Keys>(funKey: K, ...args: ArgMap[K]) {
>apply : <K extends keyof { sum: [a: number, b: number]; concat: [a: string, b: string, c: string]; }>(funKey: K, ...args: { sum: [a: number, b: number]; concat: [a: string, b: string, c: string]; }[K]) => void
>funKey : K
>args : { sum: [a: number, b: number]; concat: [a: string, b: string, c: string]; }[K]
const fn = funs[funKey];
>fn : { concat: (a: string, b: string, c: string) => void; sum: (a: number, b: number) => void; }[K]
>funs[funKey] : { concat: (a: string, b: string, c: string) => void; sum: (a: number, b: number) => void; }[K]
>funs : { concat: (a: string, b: string, c: string) => void; sum: (a: number, b: number) => void; }
>funKey : K
fn(...args);
>fn(...args) : void
>fn : { concat: (a: string, b: string, c: string) => void; sum: (a: number, b: number) => void; }[K]
>...args : string | number
>args : { sum: [a: number, b: number]; concat: [a: string, b: string, c: string]; }[K]
}
const x1 = apply('sum', 1, 2)
>x1 : void
>apply('sum', 1, 2) : void
>apply : <K extends keyof { sum: [a: number, b: number]; concat: [a: string, b: string, c: string]; }>(funKey: K, ...args: { sum: [a: number, b: number]; concat: [a: string, b: string, c: string]; }[K]) => void
>'sum' : "sum"
>1 : 1
>2 : 2
const x2 = apply('concat', 'str1', 'str2', 'str3' )
>x2 : void
>apply('concat', 'str1', 'str2', 'str3' ) : void
>apply : <K extends keyof { sum: [a: number, b: number]; concat: [a: string, b: string, c: string]; }>(funKey: K, ...args: { sum: [a: number, b: number]; concat: [a: string, b: string, c: string]; }[K]) => void
>'concat' : "concat"
>'str1' : "str1"
>'str2' : "str2"
>'str3' : "str3"
}
// Repro from #47368
type ArgMap = { a: number, b: string };
>ArgMap : ArgMap
>a : number
>b : string
type Func<K extends keyof ArgMap> = (x: ArgMap[K]) => void;
>Func : Func<K>
>x : ArgMap[K]
type Funcs = { [K in keyof ArgMap]: Func<K> };
>Funcs : Funcs
function f1<K extends keyof ArgMap>(funcs: Funcs, key: K, arg: ArgMap[K]) {
>f1 : <K extends keyof ArgMap>(funcs: Funcs, key: K, arg: ArgMap[K]) => void
>funcs : Funcs
>key : K
>arg : ArgMap[K]
funcs[key](arg);
>funcs[key](arg) : void
>funcs[key] : Funcs[K]
>funcs : Funcs
>key : K
>arg : ArgMap[K]
}
function f2<K extends keyof ArgMap>(funcs: Funcs, key: K, arg: ArgMap[K]) {
>f2 : <K extends keyof ArgMap>(funcs: Funcs, key: K, arg: ArgMap[K]) => void
>funcs : Funcs
>key : K
>arg : ArgMap[K]
const func = funcs[key]; // Type Funcs[K]
>func : Funcs[K]
>funcs[key] : Funcs[K]
>funcs : Funcs
>key : K
func(arg);
>func(arg) : void
>func : Funcs[K]
>arg : ArgMap[K]
}
function f3<K extends keyof ArgMap>(funcs: Funcs, key: K, arg: ArgMap[K]) {
>f3 : <K extends keyof ArgMap>(funcs: Funcs, key: K, arg: ArgMap[K]) => void
>funcs : Funcs
>key : K
>arg : ArgMap[K]
const func: Func<K> = funcs[key]; // Error, Funcs[K] not assignable to Func<K>
>func : Func<K>
>funcs[key] : Funcs[K]
>funcs : Funcs
>key : K
func(arg);
>func(arg) : void
>func : Func<K>
>arg : ArgMap[K]
}
function f4<K extends keyof ArgMap>(x: Funcs[keyof ArgMap], y: Funcs[K]) {
>f4 : <K extends keyof ArgMap>(x: Funcs[keyof ArgMap], y: Funcs[K]) => void
>x : Func<"b"> | Func<"a">
>y : Funcs[K]
x = y;
>x = y : Funcs[K]
>x : Func<"b"> | Func<"a">
>y : Funcs[K]
}
// Repro from #47890
interface MyObj {
someKey: {
>someKey : { name: string; }
name: string;
>name : string
}
someOtherKey: {
>someOtherKey : { name: number; }
name: number;
>name : number
}
}
const ref: MyObj = {
>ref : MyObj
>{ someKey: { name: "" }, someOtherKey: { name: 42 }} : { someKey: { name: string; }; someOtherKey: { name: number; }; }
someKey: { name: "" },
>someKey : { name: string; }
>{ name: "" } : { name: string; }
>name : string
>"" : ""
someOtherKey: { name: 42 }
>someOtherKey : { name: number; }
>{ name: 42 } : { name: number; }
>name : number
>42 : 42
};
function func<K extends keyof MyObj>(k: K): MyObj[K]['name'] | undefined {
>func : <K extends keyof MyObj>(k: K) => MyObj[K]['name'] | undefined
>k : K
const myObj: Partial<MyObj>[K] = ref[k];
>myObj : Partial<MyObj>[K]
>ref[k] : MyObj[K]
>ref : MyObj
>k : K
if (myObj) {
>myObj : Partial<MyObj>[K]
return myObj.name;
>myObj.name : string | number
>myObj : { name: string; } | { name: number; }
>name : string | number
}
const myObj2: Partial<MyObj>[keyof MyObj] = ref[k];
>myObj2 : { name: string; } | { name: number; } | undefined
>ref[k] : { name: string; } | { name: number; }
>ref : MyObj
>k : K
if (myObj2) {
>myObj2 : { name: string; } | { name: number; }
return myObj2.name;
>myObj2.name : string | number
>myObj2 : { name: string; } | { name: number; }
>name : string | number
}
return undefined;
>undefined : undefined
}