Microsoft/TypeScript
switch-true case-true narrowing not equivalent to default case.
开放
#59,133 创建于 2024年7月3日
Domain: check: Control FlowHelp WantedPossible Improvement
仓库指标
- 星标
- (108,860 个星标)
- PR 合并指标
- (PR 指标待抓取)
描述
🔎 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?