microsoft / microsoft/TypeScript
False positive for "Unreachable code detected" for code following `switch`/`case` with `allowUnreachableCode: false`
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
"false positive allowUnreachableCode"
"allowUnreachableCode case statement"
"Unreachable code detected case statement"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
switch/case
⏯ Playground Link
💻 Code
type Foo = "bar" | "baz"
const fn = (value: Foo): number => {
switch(value) {
case "bar":
return 1;
case "baz":
return 2;
}
return assertNever("nope!");
}
🙁 Actual behavior
return assertNever("nope!");
is highlighted as "Unreachable code detected". The docs, however, state:
This does not affect errors on the basis of code which appears to be unreachable due to type analysis.
🙂 Expected behavior
No error. This should be the same as the other case form, the if form, and various similar constructs where even though it's allegedly exhaustive, that's just based off of type information (I would like to have a runtime assertion in this case).
Additional information about the issue
Possibly related to: https://github.com/microsoft/TypeScript/issues/18882
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and compare the switch/case example with the other case forms described in the report. Review the possibly related issue #18882, then verify that the runtime assertion after the exhaustive switch is no longer reported as unreachable while the existing unreachable-code behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100