microsoft / microsoft/TypeScript

False positive for "Unreachable code detected" for code following `switch`/`case` with `allowUnreachableCode: false`

Offen
#59,876 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 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

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAYg9nKBeKAiARgQwE6qgHzSwC9UAoMgYzgDsBnYKAMxuSgAoA3TAGwFcIALlgIAlMJp8AtugjZkAPigBvMlHVQ6AdwCWwSgAsuvAaJVqNlypjrQMOVIIuWXUbBGB9srAIwBuZxdrWyJMUidXV3dPbygAJgDLAF8KS2ivVhtbbGAAOQhOOXZUGjhIAEJUUQCUqloGZho4tmN+IRE4cShJGTlFc0ttPUNW0wHI4LssXAjItI8MqH9AqxspsMcVyPTYhK31ABMIJkw+HmBZufUdzLpsvIKikrKISurAlNrqekYWAGYWtw2sJ4J0JNJZPIkEpVJYdExRtAkMjQrgzLDtgtYstklAIDwQvCOECBMgUfZSOj9m4saw9slAoEblAsnIHoVsMVShUqjUKEw+DRKMAdLQWXc2fkOewpBA7pgAOYQAD8wgY2B0NAVXRoj3kGOABmwcC03QgpoAothjZzZfKlVBlcq0AAVAw6OhQahHKBaGx4gAekGFEAOUGAiFkUEF7kwhkw6B4EAAdLyyLUWMUEGA6Hh-aDRGQWHEs2Vc+KOoX-qWc3nPQWyEA

💻 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der verlinkten TypeScript Playground-Reproduktion und vergleiche das switch/case-Beispiel mit den anderen im Bericht beschriebenen case-Formen. Prüfe anschließend das möglicherweise verwandte Issue #18882 und verifiziere dann, dass die Laufzeitassertion nach dem erschöpfenden switch nicht mehr als unerreichbar gemeldet wird, während das bestehende Verhalten für unerreichbaren Code erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.