microsoft / microsoft/TypeScript
incorrect inference in union of tuple types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
tuple type union, inference, discriminator
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics.
⏯ Playground Link
💻 Code
type And<T> = ["and", Term<T>, Term<T>]
type Eq<T> = ["eq", T]
type Its<T, K extends keyof T> = ["its", K, Term<T[K]>]
type Term<T> = Eq<T> | Its<T, keyof T> | And<T>
function predicate<T>(term: Term<T>) {
return term
}
const t: Term<{a:number}> = predicate(["its", "a", ["eq", 0]])
🙁 Actual behavior
TypeScript complains that Type "a" is not assignable to type number | "toString" | "toFixed" | "toExponential" | "toPrecision" | "valueOf" | "toLocaleString" | Term<number>. The expected types are a union of number from Eq<number>, Term<T> from And<T> and keyof number from Its<number>. Eq<number> can be ruled out by both length and first element and And<T> by the first element. number was chosen as value of T despite there being a solution that is also clear for the expected return type.
🙂 Expected behavior
TypeScript infers T to be { a: number } (or { a: any } when the return type is not known).
Additional information about the issue
It works when you remove And from Term, moving the "and" to the last index or when modelling something similar using objects instead of tuples. The first element could be used to discriminate the union.
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
Riproduci il problema di inferenza dell’unione di tuple nel TypeScript Playground collegato usando i tipi Term, Eq, Its e And forniti. Analizza come viene usato il primo elemento della tupla durante l’inferenza del parametro generico attraverso l’unione. Il lavoro è completato quando l’esempio inferisce T come { a: number } (o come { a: any } senza un tipo di ritorno contestuale), preservando al contempo i casi di tuple esistenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100