File: intersectionNarrowing.js

package info (click to toggle)
node-typescript 5.0.4%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 459,116 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (78 lines) | stat: -rw-r--r-- 1,620 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
//// [intersectionNarrowing.ts]
// Repros from #43130

function f1<T>(x: T & string | T & undefined) {
    if (x) {
        x;  // Should narrow to T & string
    }
}

function f2<T>(x: T & string | T & undefined) {
    if (x !== undefined) {
        x;  // Should narrow to T & string
    }
    else {
        x;  // Should narrow to T & undefined
    }
}

function f3<T>(x: T & string | T & number) {
    if (typeof x === "string") {
        x;  // Should narrow to T & string
    }
    else {
        x;  // Should narrow to T & number
    }
}

function f4<T>(x: T & 1 | T & 2) {
    switch (x) {
        case 1: x; break;  // T & 1
        case 2: x; break;  // T & 2
        default: x;  // Should narrow to never
    }
}

function f5<T extends string | number>(x: T & number) {
    const t1 = x === "hello";  // Should be an error
}


//// [intersectionNarrowing.js]
"use strict";
// Repros from #43130
function f1(x) {
    if (x) {
        x; // Should narrow to T & string
    }
}
function f2(x) {
    if (x !== undefined) {
        x; // Should narrow to T & string
    }
    else {
        x; // Should narrow to T & undefined
    }
}
function f3(x) {
    if (typeof x === "string") {
        x; // Should narrow to T & string
    }
    else {
        x; // Should narrow to T & number
    }
}
function f4(x) {
    switch (x) {
        case 1:
            x;
            break; // T & 1
        case 2:
            x;
            break; // T & 2
        default: x; // Should narrow to never
    }
}
function f5(x) {
    var t1 = x === "hello"; // Should be an error
}