microsoft / microsoft/TypeScript
Types that depend on constants are not narrowing correctly
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
"ternary narrowing", "ternary constant", "constant narrowing", "constant dependent"
🕗 Version & Regression Information
- This is the behavior in every version I tried (including nightly), and I reviewed the FAQ.
⏯ Playground Link
💻 Code
const BOOL = true as const
type T_True = { x: string, T: string }
type T_False = { x: string, F: string }
type T = (typeof BOOL) extends true ? T_True : T_False
function f(): T {
const varT: T_True = { x: 'varT', T: 'good' }
const varF: T_False = { x: 'varF', F: 'okay' }
return BOOL ? varT : varF
}
🙁 Actual behavior
Typechecking fails with the error Type 'T_True | T_False' is not assignable to type 'T_True'. Property 'T' is missing in type 'T_False' but required in type 'T_True'. on the return statement.
🙂 Expected behavior
Typechecking should pass, since the const BOOL = true as const should mark the false ternary case as unreachable.
I also tried making T generic with respect to BOOL, and splitting the ternary operator into an if/else, but the same error persisted regardless.
Additional information about the issue
No response
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 eseguendo il TypeScript Playground collegato con l'esempio fornito di BOOL, tipo condizionale e ternario per riprodurre l'errore dell'istruzione return. Traccia il comportamento del controllo dei tipi per le condizioni costanti; il lavoro è completato quando l'esempio supera il controllo dei tipi con il ramo false trattato come irraggiungibile.
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