microsoft / microsoft/TypeScript
Recursive tuple cannot be computed through generic, but valid in a declarative form
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
🔎 Search Terms
ts2589 "Type instantiation is excessively deep and possibly infinite"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about ts2589
- I was unable to test this on prior versions before v4.7.0 because
infer T extends Rsyntax not introduced
⏯ Playground Link
💻 Code
type Foo = {
leading: 'a',
rest: [Foo],
};
type Expand<T> = T extends {
leading: infer L;
rest: infer R extends any[]
} ? [L, ...ExpandMany<R>] : never;
type ExpandMany<T extends any[]> =
T extends [infer First, ...infer Rest] ? [Expand<First>, ...ExpandMany<Rest>] : [];
type TestA = Expand<Foo>;
// ^~ Type instantiation is excessively deep and possibly infinite.(2589)
type TestB = ['a', TestB];
🙁 Actual behavior
TestA meant to be:
TestA
-> Expand<Foo>
-> ['a', ...ExpandMany<[Foo]>]
-> ['a', ...[Expand<Foo>]]
-> ['a', Expand<Foo>]
-> ['a', TestA]
which is identical to TestB but TS complains the instantiation cannot stop.
🙂 Expected behavior
Stop expansion at first reference at appeared structure Expand<Foo>, that is:
type TestA = Expand<Foo>;
// ^?~ type TestA = ['a', Expand<Foo>]
// or more intelligent `type TestA = ['a', TestA]`
Additional information about the issue
This code is useful when Foo is as const inferred from a runtime constant, and we want a tuple-like typing calculated from Foo. Since TypeScript support self-referential tuple by declarative just like TestB, it would be better for support that in a computed way like Expand<Foo>.
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
Comienza con el ejemplo enlazado de TypeScript Playground y confirma el error TS2589 para TestA, mientras que TestB se acepta. Traza cómo se instancian los tipos condicionales y de tupla recursivos, y define la finalización de modo que la forma calculada pueda estabilizarse en una tupla autorreferencial sin introducir regresiones en los diagnósticos de recursión.
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
- 35/100