Exhaustive switch case with returns can mark a finally block as unreachable
#61,259 opened on 2025幎2æ24æ¥
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð Search Terms
finally switch exhaustive unreachable
ð Version & Regression Information
This is the behavior in every version I tried ( doesn't typecheck pre 3.7 ), and I reviewed the FAQ for entries about unreachable code
⯠Playground Link
ð» Code
const c = (v: 1 | 2) => {
try {
switch (v) {
case 1: return v
case 2: return v
}
} finally {
if (true) {
console.log('exit')
}
}
}
ð Actual behavior
The contents of the finally blocked are marked as unreachable
ð Expected behavior
That they are reachable
Additional information about the issue
While the example seems to include some confusing additions the replication apparently requires:
- The cases to be exhaustive
- No default case
- For the switch expression be typed as a literal union of some form
- An
ifstatement to be in thefinallyblock
I assume this is a chaotic mix of unreachable patterns upsetting flow analysis. I've also noticed that it you add an unreachable return statement after the switch then the contents of finally become reachable.