Closed
Description
Bug Report
🔎 Search Terms
- switch narrowing
- switch(true)
🔗 Related issues
- Narrowing types by
typeof
insideswitch (true)
does not work #37178 - Control flow analysis:
switch (true)
case #8934
Maybe these ones too
- Alias type conditions to desctrutured variables #46143
- Generic type unnarrowed by
switch...case
#43873
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type guards
⏯ Playground Link
Playground link with relevant code
💻 Code
const char = "(" as string;
// This works fine, char inside the if statements get's property narrowed to type "("
if (char === '(') {
char;
}
// This doesn't work, char inside the case statement is of type string
switch (true) {
case char === '(':
char;
}
🙁 Actual behavior
Char inside the case statement in a switch(true)
has a type string
🙂 Expected behavior
It should have type "("
Activity
MartinJohns commentedon Oct 30, 2021
Duplicate of #37178. Used search terms:
switch true in:title