Microsoft/TypeScript
GitHub ã§èŠãswitch-true case-true narrowing not equivalent to default case.
Open
#59,133 opened on 2024幎7æ3æ¥
Domain: check: Control FlowHelp WantedPossible Improvement
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð Search Terms
"switch true" "switch-true" switch true narrowing. Most issues that turned up were from 2023 which I think precedes the release of the switch-true type narrowing feature?
ð Version & Regression Information
- This is a gap
⯠Playground Link
ð» Code
function uhoh(input: string) : "A"|"B"|"C" {
switch (true) {
case /a/.test(input):
return "A";
case /b/.test(input):
return "B";
case true: // <--- default: works fine here
return "C";
}
}
ð Actual behavior
Complains that the result of uhoh could be undefined.
ð Expected behavior
switch(true) { case true: } should behave like switch(true) { default: }, probably?