microsoft / microsoft/TypeScript
Narrowing doesn't recognize string constant as truthy
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.7-beta
Search Terms: cfa narrowing string truthy
Code
declare var a: { b: number } | null
!a || a.b > 3; // works
!a && true || a.b > 3 && true; // works
!a && "a was not defined" || a.b > 3 && "a.b was too big"; // error "Object is possibly null"
Expected behavior:
The narrowings all work
Actual behavior:
The third narrowing does not work and produces an error
Playground Link:
Playground
Related Issues:
I suspect this is a duplicate, as most CFA narrowing bugs are. But I searched as best I could and didn't find any other bugs that matched what was going on here. It seems like typescript doesn't understand that a non-empty string-literal is true in this situation?
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 con la riproduzione collegata di TypeScript Playground usando TypeScript 3.7-beta e confronta le tre espressioni di narrowing. Analizza l’analisi del flusso di controllo per i letterali stringa non vuoti nel type checker; il lavoro è completato quando la terza espressione restringe a in modo da escludere null senza generare un errore e il comportamento è coperto da un test di regressione.
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