File: exhaustiveSwitchStatements1.types

package info (click to toggle)
node-typescript 5.0.4%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 459,140 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (735 lines) | stat: -rw-r--r-- 14,181 bytes parent folder | download | duplicates (3)
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
=== tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts ===
function f1(x: 1 | 2): string {
>f1 : (x: 1 | 2) => string
>x : 1 | 2

    if (!!true) {
>!!true : true
>!true : false
>true : true

        switch (x) {
>x : 1 | 2

            case 1: return 'a';
>1 : 1
>'a' : "a"

            case 2: return 'b';
>2 : 2
>'b' : "b"
        }
        x;  // Unreachable
>x : never
    }
    else {
        throw 0;
>0 : 0
    }
}

function f2(x: 1 | 2) {
>f2 : (x: 1 | 2) => void
>x : 1 | 2

    let z: number;
>z : number

    switch (x) {
>x : 1 | 2

        case 1: z = 10; break;
>1 : 1
>z = 10 : 10
>z : number
>10 : 10

        case 2: z = 20; break;
>2 : 2
>z = 20 : 20
>z : number
>20 : 20
    }
    z;  // Definitely assigned
>z : number
}

function f3(x: 1 | 2) {
>f3 : (x: 1 | 2) => 10 | 20
>x : 1 | 2

    switch (x) {
>x : 1 | 2

        case 1: return 10;
>1 : 1
>10 : 10

        case 2: return 20;
>2 : 2
>20 : 20

        // Default considered reachable to allow defensive coding
        default: throw new Error("Bad input");
>new Error("Bad input") : Error
>Error : ErrorConstructor
>"Bad input" : "Bad input"
    }
}

// Repro from #11572

enum E { A, B }
>E : E
>A : E.A
>B : E.B

function f(e: E): number {
>f : (e: E) => number
>e : E

    switch (e) {
>e : E

        case E.A: return 0
>E.A : E.A
>E : typeof E
>A : E.A
>0 : 0

        case E.B: return 1
>E.B : E.B
>E : typeof E
>B : E.B
>1 : 1
    }
}

function g(e: E): number {
>g : (e: E) => number
>e : E

    if (!true)
>!true : false
>true : true

        return -1
>-1 : -1
>1 : 1

    else
        switch (e) {
>e : E

            case E.A: return 0
>E.A : E.A
>E : typeof E
>A : E.A
>0 : 0

            case E.B: return 1
>E.B : E.B
>E : typeof E
>B : E.B
>1 : 1
        }
}

// Repro from #12668

interface Square { kind: "square"; size: number; }
>kind : "square"
>size : number

interface Rectangle { kind: "rectangle"; width: number; height: number; }
>kind : "rectangle"
>width : number
>height : number

interface Circle { kind: "circle"; radius: number; }
>kind : "circle"
>radius : number

interface Triangle { kind: "triangle"; side: number; }
>kind : "triangle"
>side : number

type Shape = Square | Rectangle | Circle | Triangle;
>Shape : Square | Rectangle | Circle | Triangle

function area(s: Shape): number {
>area : (s: Shape) => number
>s : Shape

    let area;
>area : any

    switch (s.kind) {
>s.kind : "square" | "rectangle" | "circle" | "triangle"
>s : Shape
>kind : "square" | "rectangle" | "circle" | "triangle"

        case "square": area = s.size * s.size; break;
>"square" : "square"
>area = s.size * s.size : number
>area : any
>s.size * s.size : number
>s.size : number
>s : Square
>size : number
>s.size : number
>s : Square
>size : number

        case "rectangle": area = s.width * s.height; break;
>"rectangle" : "rectangle"
>area = s.width * s.height : number
>area : any
>s.width * s.height : number
>s.width : number
>s : Rectangle
>width : number
>s.height : number
>s : Rectangle
>height : number

        case "circle": area = Math.PI * s.radius * s.radius; break;
>"circle" : "circle"
>area = Math.PI * s.radius * s.radius : number
>area : any
>Math.PI * s.radius * s.radius : number
>Math.PI * s.radius : number
>Math.PI : number
>Math : Math
>PI : number
>s.radius : number
>s : Circle
>radius : number
>s.radius : number
>s : Circle
>radius : number

        case "triangle": area = Math.sqrt(3) / 4 * s.side * s.side; break;
>"triangle" : "triangle"
>area = Math.sqrt(3) / 4 * s.side * s.side : number
>area : any
>Math.sqrt(3) / 4 * s.side * s.side : number
>Math.sqrt(3) / 4 * s.side : number
>Math.sqrt(3) / 4 : number
>Math.sqrt(3) : number
>Math.sqrt : (x: number) => number
>Math : Math
>sqrt : (x: number) => number
>3 : 3
>4 : 4
>s.side : number
>s : Triangle
>side : number
>s.side : number
>s : Triangle
>side : number
    }
    return area;
>area : number
}

function areaWrapped(s: Shape): number {
>areaWrapped : (s: Shape) => number
>s : Shape

    let area;
>area : any

    area = (() => {
>area = (() => {        switch (s.kind) {            case "square": return s.size * s.size;            case "rectangle": return s.width * s.height;            case "circle": return Math.PI * s.radius * s.radius;            case "triangle": return Math.sqrt(3) / 4 * s.side * s.side;        }    })() : number
>area : any
>(() => {        switch (s.kind) {            case "square": return s.size * s.size;            case "rectangle": return s.width * s.height;            case "circle": return Math.PI * s.radius * s.radius;            case "triangle": return Math.sqrt(3) / 4 * s.side * s.side;        }    })() : number
>(() => {        switch (s.kind) {            case "square": return s.size * s.size;            case "rectangle": return s.width * s.height;            case "circle": return Math.PI * s.radius * s.radius;            case "triangle": return Math.sqrt(3) / 4 * s.side * s.side;        }    }) : () => number
>() => {        switch (s.kind) {            case "square": return s.size * s.size;            case "rectangle": return s.width * s.height;            case "circle": return Math.PI * s.radius * s.radius;            case "triangle": return Math.sqrt(3) / 4 * s.side * s.side;        }    } : () => number

        switch (s.kind) {
>s.kind : "square" | "rectangle" | "circle" | "triangle"
>s : Shape
>kind : "square" | "rectangle" | "circle" | "triangle"

            case "square": return s.size * s.size;
>"square" : "square"
>s.size * s.size : number
>s.size : number
>s : Square
>size : number
>s.size : number
>s : Square
>size : number

            case "rectangle": return s.width * s.height;
>"rectangle" : "rectangle"
>s.width * s.height : number
>s.width : number
>s : Rectangle
>width : number
>s.height : number
>s : Rectangle
>height : number

            case "circle": return Math.PI * s.radius * s.radius;
>"circle" : "circle"
>Math.PI * s.radius * s.radius : number
>Math.PI * s.radius : number
>Math.PI : number
>Math : Math
>PI : number
>s.radius : number
>s : Circle
>radius : number
>s.radius : number
>s : Circle
>radius : number

            case "triangle": return Math.sqrt(3) / 4 * s.side * s.side;
>"triangle" : "triangle"
>Math.sqrt(3) / 4 * s.side * s.side : number
>Math.sqrt(3) / 4 * s.side : number
>Math.sqrt(3) / 4 : number
>Math.sqrt(3) : number
>Math.sqrt : (x: number) => number
>Math : Math
>sqrt : (x: number) => number
>3 : 3
>4 : 4
>s.side : number
>s : Triangle
>side : number
>s.side : number
>s : Triangle
>side : number
        }
    })();
    return area;
>area : number
}

// Repro from #13241

enum MyEnum {
>MyEnum : MyEnum

	A,
>A : MyEnum.A

	B
>B : MyEnum.B
}

function thisGivesError(e: MyEnum): string {
>thisGivesError : (e: MyEnum) => string
>e : MyEnum

	let s: string;
>s : string

	switch (e) {
>e : MyEnum

		case MyEnum.A: s = "it was A"; break;
>MyEnum.A : MyEnum.A
>MyEnum : typeof MyEnum
>A : MyEnum.A
>s = "it was A" : "it was A"
>s : string
>"it was A" : "it was A"

		case MyEnum.B: s = "it was B"; break;
>MyEnum.B : MyEnum.B
>MyEnum : typeof MyEnum
>B : MyEnum.B
>s = "it was B" : "it was B"
>s : string
>"it was B" : "it was B"
	}
	return s;
>s : string
}

function good1(e: MyEnum): string {
>good1 : (e: MyEnum) => string
>e : MyEnum

	let s: string;
>s : string

	switch (e) {
>e : MyEnum

		case MyEnum.A: s = "it was A"; break;
>MyEnum.A : MyEnum.A
>MyEnum : typeof MyEnum
>A : MyEnum.A
>s = "it was A" : "it was A"
>s : string
>"it was A" : "it was A"

		case MyEnum.B: s = "it was B"; break;
>MyEnum.B : MyEnum.B
>MyEnum : typeof MyEnum
>B : MyEnum.B
>s = "it was B" : "it was B"
>s : string
>"it was B" : "it was B"

		default: s = "it was something else"; break;
>s = "it was something else" : "it was something else"
>s : string
>"it was something else" : "it was something else"
	}
	return s;
>s : string
}

function good2(e: MyEnum): string {
>good2 : (e: MyEnum) => string
>e : MyEnum

	switch (e) {
>e : MyEnum

		case MyEnum.A: return "it was A";
>MyEnum.A : MyEnum.A
>MyEnum : typeof MyEnum
>A : MyEnum.A
>"it was A" : "it was A"

		case MyEnum.B: return "it was B";
>MyEnum.B : MyEnum.B
>MyEnum : typeof MyEnum
>B : MyEnum.B
>"it was B" : "it was B"
	}
}

// Repro from #18362

enum Level {
>Level : Level

  One,
>One : Level.One

  Two,
>Two : Level.Two
}

const doSomethingWithLevel = (level: Level) => {
>doSomethingWithLevel : (level: Level) => Level
>(level: Level) => {  let next: Level;  switch (level) {    case Level.One:      next = Level.Two;      break;    case Level.Two:      next = Level.One;      break;  }  return next;} : (level: Level) => Level
>level : Level

  let next: Level;
>next : Level

  switch (level) {
>level : Level

    case Level.One:
>Level.One : Level.One
>Level : typeof Level
>One : Level.One

      next = Level.Two;
>next = Level.Two : Level.Two
>next : Level
>Level.Two : Level.Two
>Level : typeof Level
>Two : Level.Two

      break;
    case Level.Two:
>Level.Two : Level.Two
>Level : typeof Level
>Two : Level.Two

      next = Level.One;
>next = Level.One : Level.One
>next : Level
>Level.One : Level.One
>Level : typeof Level
>One : Level.One

      break;
  }
  return next;
>next : Level

};

// Repro from #20409

interface Square2 {
    kind: "square";
>kind : "square"

    size: number;
>size : number
}

interface Circle2 {
    kind: "circle";
>kind : "circle"

    radius: number;
>radius : number
}

type Shape2 = Square2 | Circle2;
>Shape2 : Square2 | Circle2

function withDefault(s1: Shape2, s2: Shape2): string {
>withDefault : (s1: Shape2, s2: Shape2) => string
>s1 : Shape2
>s2 : Shape2

    switch (s1.kind) {
>s1.kind : "square" | "circle"
>s1 : Shape2
>kind : "square" | "circle"

        case "square":
>"square" : "square"

            return "1";
>"1" : "1"

        case "circle":
>"circle" : "circle"

            switch (s2.kind) {
>s2.kind : "square" | "circle"
>s2 : Shape2
>kind : "square" | "circle"

                case "square":
>"square" : "square"

                    return "2";
>"2" : "2"

                case "circle":
>"circle" : "circle"

                    return "3";
>"3" : "3"

                default:
                    return "never";
>"never" : "never"
            }
    }
}

function withoutDefault(s1: Shape2, s2: Shape2): string {
>withoutDefault : (s1: Shape2, s2: Shape2) => string
>s1 : Shape2
>s2 : Shape2

    switch (s1.kind) {
>s1.kind : "square" | "circle"
>s1 : Shape2
>kind : "square" | "circle"

        case "square":
>"square" : "square"

            return "1";
>"1" : "1"

        case "circle":
>"circle" : "circle"

            switch (s2.kind) {
>s2.kind : "square" | "circle"
>s2 : Shape2
>kind : "square" | "circle"

                case "square":
>"square" : "square"

                    return "2";
>"2" : "2"

                case "circle":
>"circle" : "circle"

                    return "3";
>"3" : "3"
            }
    }
}

// Repro from #20823

function test4(value: 1 | 2) {
>test4 : (value: 1 | 2) => string
>value : 1 | 2

    let x: string;
>x : string

    switch (value) {
>value : 1 | 2

        case 1: x = "one"; break;
>1 : 1
>x = "one" : "one"
>x : string
>"one" : "one"

        case 2: x = "two"; break;
>2 : 2
>x = "two" : "two"
>x : string
>"two" : "two"
    }
    return x;
>x : string
}

// Repro from #34661

enum Animal { DOG, CAT }
>Animal : Animal
>DOG : Animal.DOG
>CAT : Animal.CAT

declare const zoo: { animal: Animal } | undefined;
>zoo : { animal: Animal; } | undefined
>animal : Animal

function expression(): Animal {
>expression : () => Animal

    switch (zoo?.animal ?? Animal.DOG) {
>zoo?.animal ?? Animal.DOG : Animal
>zoo?.animal : Animal | undefined
>zoo : { animal: Animal; } | undefined
>animal : Animal | undefined
>Animal.DOG : Animal.DOG
>Animal : typeof Animal
>DOG : Animal.DOG

        case Animal.DOG: return Animal.DOG
>Animal.DOG : Animal.DOG
>Animal : typeof Animal
>DOG : Animal.DOG
>Animal.DOG : Animal.DOG
>Animal : typeof Animal
>DOG : Animal.DOG

        case Animal.CAT: return Animal.CAT
>Animal.CAT : Animal.CAT
>Animal : typeof Animal
>CAT : Animal.CAT
>Animal.CAT : Animal.CAT
>Animal : typeof Animal
>CAT : Animal.CAT
    }
}

// Repro from #34840

function foo() {
>foo : () => void

    const foo: number | undefined = 0;
>foo : number | undefined
>0 : 0

    while (true) {
>true : true

        const stats = foo;
>stats : number
>foo : number

        switch (stats) {
>stats : number

            case 1: break;
>1 : 1

            case 2: break;
>2 : 2
        }
    }
}

// Repro from #35070

type O = {
>O : { a: number; b: number; }

    a: number,
>a : number

    b: number
>b : number

};
type K = keyof O | 'c';
>K : keyof O | "c"

function ff(o: O, k: K) {
>ff : (o: O, k: K) => number
>o : O
>k : K

    switch(k) {
>k : K

        case 'c':
>'c' : "c"

            k = 'a';
>k = 'a' : "a"
>k : K
>'a' : "a"
    }
    k === 'c';  // Error
>k === 'c' : boolean
>k : keyof O
>'c' : "c"

    return o[k];
>o[k] : number
>o : O
>k : keyof O
}

// Repro from #35431
type A = { kind: "abc" } | { kind: "def" };
>A : { kind: "abc"; } | { kind: "def"; }
>kind : "abc"
>kind : "def"

function f35431(a: A) {
>f35431 : (a: A) => void
>a : A

  switch (a.kind) {
>a.kind : "abc" | "def"
>a : A
>kind : "abc" | "def"

    case "abc":
>"abc" : "abc"

    case "def": return;
>"def" : "def"

    default:
      a!.kind; // Error expected
>a!.kind : any
>a! : never
>a : never
>kind : any
  }
}