You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Narrowing switch(true) doesnt work for default case and is wrong for fallthrough cases in 5.3 #55986
BugA bug in TypeScriptEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Fix AvailableA PR has been opened for this issue
type foo = 'a' | 'b' | 'c'
const f: foo = 'a' as any
switch(true) {
case f === 'a':
console.log(f)
// ^? "a"
// falls through
case f === 'b':
console.log(f)
// ^? "a" | "b"
break
default:
console.log(f)
// ^? foo
}
This comes down to the fact that the PR didn't handle multiple clauses at all... I should have known the PR was too simple 😅
I have a fix for the multiple clause cases, but default, not so sure about. May need to just be unnarrowed there as I'm not sure we have a mechanism to narrow like we do the other switch case instances as those do the narrowing directly without control flow, whereas this has to be more like if statements or something. I guess we'll have something like this eventually for matching...
BugA bug in TypeScriptEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Fix AvailableA PR has been opened for this issue
Activity
DanielRosenwasser commentedon Oct 5, 2023
Bizarre that it might have something to do with empty case clauses
Playground Link
jakebailey commentedon Oct 5, 2023
Mateusz Burzyński (@Andarist) if he beats me to it before tomorrow
fatcerberus commentedon Oct 5, 2023
Daniel Rosenwasser (@DanielRosenwasser) fwiw while your code narrows correctly for the
cases, it still getsdefaultwrong (it should narrow to"c")Also when did
switch (true)start working? (this pattern is an irrational pet peeve of mine but that's neither here nor there)DanielRosenwasser commentedon Oct 5, 2023
I also think it's a little hokey but #53681
jakebailey commentedon Oct 5, 2023
This comes down to the fact that the PR didn't handle multiple clauses at all... I should have known the PR was too simple 😅
I have a fix for the multiple clause cases, but default, not so sure about. May need to just be unnarrowed there as I'm not sure we have a mechanism to narrow like we do the other switch case instances as those do the narrowing directly without control flow, whereas this has to be more like if statements or something. I guess we'll have something like this eventually for matching...
jakebailey commentedon Oct 5, 2023
PR at #55991.