microsoft / microsoft/TypeScript
incorrect inference in union of tuple types
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
🔎 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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Reproduce el fallo de inferencia de unión de tuplas en el TypeScript Playground enlazado usando los tipos Term, Eq, Its y And proporcionados. Investiga cómo se usa el primer elemento de la tupla al inferir el parámetro genérico a través de la unión. Se considera terminado cuando el ejemplo infiere T como { a: number } (o como { a: any } sin un tipo de retorno contextual), preservando al mismo tiempo los casos de tuplas existentes.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100