microsoft / microsoft/TypeScript
Incorrect union type inference for conjunction with strictNullChecks disabled
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: 3.4.5
Search Terms: union type inference conjunction strictNullChecks
Code
function fn(x: number) {
return x && 'a'
}
Expected behavior:
Return type of fn should be 0 | 'a'
Actual behavior:
Return type of fn is '' | 'a' if strictNullChecks is disabled
This is causing me an issue with this specific bit of code, where I'm checking window so my code runs both on the browser and on the client:
function fn(x: number): OrientationType | undefined {
return window && (window.outerWidth > window.outerHeight ? 'landscape-primary' : 'portrait-primary')
}
I can replace the conjunction with a conditional expression, and I know that if strictNullChecks is disabled I should have a fallback for the return value but I'm posting here for the sake of trying to understand if this is working as intended. It also feels weird that my example compiles with strictNullChecks enabled but doesn't compile when it's disabled.
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 gli esempi nel TypeScript Playground collegato con strictNullChecks disabilitato e abilitato. Traccia come il type checker inferisce l’espressione di congiunzione in fn(x: number). Il lavoro è completato quando l’impostazione disabilitata produce il tipo di ritorno previsto 0 | 'a' senza compromettere l’impostazione abilitata o l’esempio window.
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