Type inference worked in 4.2.3 but fails in 4.3.2
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- typescript
- Ambito
- compilers
Direzione di ricerca
Inizia con il repro collegato in TypeScript Playground e confronta l’inferenza tra le versioni 4.2.3 e 4.3.2, concentrandoti sulla scelta di null nell’array choices e sulla chiamata a promptChoice. Il lavoro è completato quando l’esempio non segnala più l’errore di assegnazione della tupla e choice viene inferito come 'one'|'two'|'three'.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Bug Report
🔎 Search Terms
type inference
🕗 Version & Regression Information
4.3.2
- This is a crash
[x] This changed between versions 4.2.3 and 4.3.2 - This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
⏯ Playground Link
Playground link with relevant code
💻 Code
(strict null checks off)
interface PromptChoiceOptions<T extends string> {
label: string;
button: string;
default?: T;
choices: readonly ( T | [ string, T ] )[];
}
function promptChoice<T extends string>( options?: Partial<PromptChoiceOptions<T>> ) {
return options?.choices?.[ 0 ] ?? null; // simplified
}
const choice = promptChoice( { label: 'example', button: 'select', choices: [
[ 'null', null ], // problem seems to be related to this line
[ 'One', 'one' ],
[ 'Two', 'two' ],
[ 'Three', 'three' ]
] } );
🙁 Actual behavior
Type [string, "two"] is not assignable to type [string, "one"]
🙂 Expected behavior
choice is inferred to be 'one'|'two'|'three'
In creating a minimal repro for this, I discovered that commenting out the null choice makes the code behave properly. Also, changing the type of choices to readonly ( T | [ string, T|null] )[]; with strict null checking makes the problem always occur.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 15h
- PR unite (30g)
- 106
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.
Altre issue di microsoft/TypeScript
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
microsoft/TypeScript#64322 · 2 commenti · 1 reazione · 2 assegnatari ·
-
Possible Improvement
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
microsoft/TypeScript#64278 · 1 commento · 1 reazione ·
-
Docs
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
microsoft/TypeScript#64118 · 1 commento ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
microsoft/TypeScript#64094 ·
-
Docs
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
microsoft/TypeScript#63959 · 5 commenti ·