Bug Report
🔎 Search Terms
while switch union narrowing never
🕗 Version & Regression Information
Version: 4.5.4
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about while, loop, switch
⏯ Playground Link
Playground link with relevant code
💻 Code
let foo: "aaa" | "bbb" = "aaa";
while (true) {
switch (foo) {
case "aaa":
1+1; //any statement
break;
case "bbb":
1+1; //any statement
break;
}
if (foo === "aaa") {
console.log("Foo is aaa");
foo = "bbb";
} else if (foo === "bbb") { //says foo is never
console.log("Foo is bbb");
console.log("Loop is done");
break;
}
}
🙁 Actual behavior
On the line } else if (foo === "bbb") { when you hover over foo typescript thinks the type is never. If that was the case then you'd expect nothing inside that block to run.
🙂 Expected behavior
foo should be type "bbb" here, and if you remove the switch statement it is. Furthermore if you run the compiled JS, it does hit all the console.log lines and produces
Foo is aaa
Foo is bbb
Loop is done
Bug Report
🔎 Search Terms
while switch union narrowing never
🕗 Version & Regression Information
Version: 4.5.4
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
On the line
} else if (foo === "bbb") {when you hover overfootypescript thinks the type is never. If that was the case then you'd expect nothing inside that block to run.🙂 Expected behavior
fooshould be type"bbb"here, and if you remove the switch statement it is. Furthermore if you run the compiled JS, it does hit all the console.log lines and produces