microsoft / microsoft/TypeScript

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

Aperta
#59,876 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione collegata in TypeScript Playground e confronta l’esempio di switch/case con le altre forme di case descritte nel report. Esamina il possibile issue correlato #18882, quindi verifica che l’asserzione a runtime dopo lo switch esaustivo non venga più segnalata come irraggiungibile, mentre il comportamento esistente per il codice irraggiungibile rimane invariato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.