microsoft / microsoft/TypeScript
narrowing to "never" by "if" statement does not exclude "undefined" return type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TypeScript Version: 2.7.1
Search Terms: narrowing types never switch case if undefined strictNullChecks
Code
// Build this code with "--strictNullChecks" option.
function assertNever(x: never): never {
throw new Error("not reached");
}
enum A {
Foo, Bar
}
// There are no errors on this function.
function good(a: A): number {
switch (a) {
case A.Foo: return 0;
case A.Bar: return 1;
}
}
// This function causes the following error though "a" is "never" at the end of the function.
// error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
function bad(a: A): number {
if (a === A.Foo) return 0;
if (a === A.Bar) return 1;
}
// No error if explicit return of "never" is added.
function ok(a: A): number {
if (a === A.Foo) return 0;
if (a === A.Bar) return 1;
return assertNever(a);
}
Expected behavior: The bad() function should not cause error so that if statement works like switch.
Actual behavior: As commented in the code, only bad() function causes an error.
Playground Link: (please check "strictNullChecks" option.)
Related Issues:
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo il codice TypeScript dell'issue con strictNullChecks abilitato e confronta i casi if e switch. Traccia il narrowing del control-flow del compilatore e l'analisi dei ritorni mancanti per la funzione errata. Il lavoro è completo quando la diagnostica segnalata è risolta per questo caso e la copertura di regressione conferma il comportamento.
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
- Specificata chiaramente
- Idoneità per principianti
- 35/100