|
| 1 | +//// [tests/cases/compiler/narrowByClauseExpressionInSwitchTrue.ts] //// |
| 2 | + |
| 3 | +=== narrowByClauseExpressionInSwitchTrue.ts === |
| 4 | +// https://github.com/microsoft/TypeScript/issues/37178 |
| 5 | + |
| 6 | +type A = { type: "A" }; |
| 7 | +>A : Symbol(A, Decl(narrowByClauseExpressionInSwitchTrue.ts, 0, 0)) |
| 8 | +>type : Symbol(type, Decl(narrowByClauseExpressionInSwitchTrue.ts, 2, 10)) |
| 9 | + |
| 10 | +type B = { type: "B" }; |
| 11 | +>B : Symbol(B, Decl(narrowByClauseExpressionInSwitchTrue.ts, 2, 23)) |
| 12 | +>type : Symbol(type, Decl(narrowByClauseExpressionInSwitchTrue.ts, 3, 10)) |
| 13 | + |
| 14 | +type AorB = A | B; |
| 15 | +>AorB : Symbol(AorB, Decl(narrowByClauseExpressionInSwitchTrue.ts, 3, 23)) |
| 16 | +>A : Symbol(A, Decl(narrowByClauseExpressionInSwitchTrue.ts, 0, 0)) |
| 17 | +>B : Symbol(B, Decl(narrowByClauseExpressionInSwitchTrue.ts, 2, 23)) |
| 18 | + |
| 19 | +const isA = (x: AorB): x is A => x.type === "A"; |
| 20 | +>isA : Symbol(isA, Decl(narrowByClauseExpressionInSwitchTrue.ts, 6, 5)) |
| 21 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 6, 13)) |
| 22 | +>AorB : Symbol(AorB, Decl(narrowByClauseExpressionInSwitchTrue.ts, 3, 23)) |
| 23 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 6, 13)) |
| 24 | +>A : Symbol(A, Decl(narrowByClauseExpressionInSwitchTrue.ts, 0, 0)) |
| 25 | +>x.type : Symbol(type, Decl(narrowByClauseExpressionInSwitchTrue.ts, 2, 10), Decl(narrowByClauseExpressionInSwitchTrue.ts, 3, 10)) |
| 26 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 6, 13)) |
| 27 | +>type : Symbol(type, Decl(narrowByClauseExpressionInSwitchTrue.ts, 2, 10), Decl(narrowByClauseExpressionInSwitchTrue.ts, 3, 10)) |
| 28 | + |
| 29 | +const isB = (x: AorB): x is B => x.type === "B"; |
| 30 | +>isB : Symbol(isB, Decl(narrowByClauseExpressionInSwitchTrue.ts, 7, 5)) |
| 31 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 7, 13)) |
| 32 | +>AorB : Symbol(AorB, Decl(narrowByClauseExpressionInSwitchTrue.ts, 3, 23)) |
| 33 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 7, 13)) |
| 34 | +>B : Symbol(B, Decl(narrowByClauseExpressionInSwitchTrue.ts, 2, 23)) |
| 35 | +>x.type : Symbol(type, Decl(narrowByClauseExpressionInSwitchTrue.ts, 2, 10), Decl(narrowByClauseExpressionInSwitchTrue.ts, 3, 10)) |
| 36 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 7, 13)) |
| 37 | +>type : Symbol(type, Decl(narrowByClauseExpressionInSwitchTrue.ts, 2, 10), Decl(narrowByClauseExpressionInSwitchTrue.ts, 3, 10)) |
| 38 | + |
| 39 | +function test1(x: AorB) { |
| 40 | +>test1 : Symbol(test1, Decl(narrowByClauseExpressionInSwitchTrue.ts, 7, 48)) |
| 41 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 9, 15)) |
| 42 | +>AorB : Symbol(AorB, Decl(narrowByClauseExpressionInSwitchTrue.ts, 3, 23)) |
| 43 | + |
| 44 | + switch (true) { |
| 45 | + case isA(x): |
| 46 | +>isA : Symbol(isA, Decl(narrowByClauseExpressionInSwitchTrue.ts, 6, 5)) |
| 47 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 9, 15)) |
| 48 | + |
| 49 | + x; |
| 50 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 9, 15)) |
| 51 | + |
| 52 | + break; |
| 53 | + case isB(x): |
| 54 | +>isB : Symbol(isB, Decl(narrowByClauseExpressionInSwitchTrue.ts, 7, 5)) |
| 55 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 9, 15)) |
| 56 | + |
| 57 | + x; |
| 58 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 9, 15)) |
| 59 | + |
| 60 | + break; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function test2(x: AorB) { |
| 65 | +>test2 : Symbol(test2, Decl(narrowByClauseExpressionInSwitchTrue.ts, 18, 1)) |
| 66 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 20, 15)) |
| 67 | +>AorB : Symbol(AorB, Decl(narrowByClauseExpressionInSwitchTrue.ts, 3, 23)) |
| 68 | + |
| 69 | + switch (true) { |
| 70 | + case isA(x): |
| 71 | +>isA : Symbol(isA, Decl(narrowByClauseExpressionInSwitchTrue.ts, 6, 5)) |
| 72 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 20, 15)) |
| 73 | + |
| 74 | + x; |
| 75 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 20, 15)) |
| 76 | + |
| 77 | + // fallthrough |
| 78 | + case isB(x): |
| 79 | +>isB : Symbol(isB, Decl(narrowByClauseExpressionInSwitchTrue.ts, 7, 5)) |
| 80 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 20, 15)) |
| 81 | + |
| 82 | + x; |
| 83 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 20, 15)) |
| 84 | + |
| 85 | + break; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +let x: string | undefined; |
| 90 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 31, 3)) |
| 91 | + |
| 92 | +switch (true) { |
| 93 | + case typeof x !== "undefined": |
| 94 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 31, 3)) |
| 95 | + |
| 96 | + x.trim(); |
| 97 | +>x.trim : Symbol(String.trim, Decl(lib.es5.d.ts, --, --)) |
| 98 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 31, 3)) |
| 99 | +>trim : Symbol(String.trim, Decl(lib.es5.d.ts, --, --)) |
| 100 | +} |
| 101 | + |
| 102 | +type SomeType = { type: "SomeType" }; |
| 103 | +>SomeType : Symbol(SomeType, Decl(narrowByClauseExpressionInSwitchTrue.ts, 36, 1)) |
| 104 | +>type : Symbol(type, Decl(narrowByClauseExpressionInSwitchTrue.ts, 38, 17)) |
| 105 | + |
| 106 | +declare function isSomeType(x: unknown): x is SomeType; |
| 107 | +>isSomeType : Symbol(isSomeType, Decl(narrowByClauseExpressionInSwitchTrue.ts, 38, 37)) |
| 108 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 39, 28)) |
| 109 | +>x : Symbol(x, Decl(narrowByClauseExpressionInSwitchTrue.ts, 39, 28)) |
| 110 | +>SomeType : Symbol(SomeType, Decl(narrowByClauseExpressionInSwitchTrue.ts, 36, 1)) |
| 111 | + |
| 112 | +function processInput(input: string | RegExp | SomeType) { |
| 113 | +>processInput : Symbol(processInput, Decl(narrowByClauseExpressionInSwitchTrue.ts, 39, 55)) |
| 114 | +>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue.ts, 41, 22)) |
| 115 | +>RegExp : Symbol(RegExp, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) |
| 116 | +>SomeType : Symbol(SomeType, Decl(narrowByClauseExpressionInSwitchTrue.ts, 36, 1)) |
| 117 | + |
| 118 | + switch (true) { |
| 119 | + case typeof input === "string": |
| 120 | +>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue.ts, 41, 22)) |
| 121 | + |
| 122 | + input; |
| 123 | +>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue.ts, 41, 22)) |
| 124 | + |
| 125 | + break; |
| 126 | + case input instanceof RegExp: |
| 127 | +>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue.ts, 41, 22)) |
| 128 | +>RegExp : Symbol(RegExp, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) |
| 129 | + |
| 130 | + input; |
| 131 | +>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue.ts, 41, 22)) |
| 132 | + |
| 133 | + break; |
| 134 | + case isSomeType(input): |
| 135 | +>isSomeType : Symbol(isSomeType, Decl(narrowByClauseExpressionInSwitchTrue.ts, 38, 37)) |
| 136 | +>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue.ts, 41, 22)) |
| 137 | + |
| 138 | + input; |
| 139 | +>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue.ts, 41, 22)) |
| 140 | + |
| 141 | + break; |
| 142 | + } |
| 143 | +} |
| 144 | + |
0 commit comments