microsoft / microsoft/TypeScript

Type instantiation is excessively deep with tuple spread

Abierto
#54,910 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Domain: Conditional Types Help Wanted Possible Improvement
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Bug Report

🔎 Search Terms
  • 2889 spread
🕗 Version & Regression Information
  • This is the behavior in every version I tried (4.1.5 to 5.2.0 nightly), and I reviewed the FAQ for entries about: recursion
⏯ Playground Link

Playground link with relevant code

💻 Code
type NTuple<T, N extends number, Acc extends T[] = []> =
  N extends Acc['length']
  ? Acc
  : NTuple<T, N, [...Acc, T]>

type Pair = NTuple<number, 2>;
//   ^? type Pair = [number, number]

type SpreadN<A extends number> = [...NTuple<any, A>]
//                               ~~~~~~~~~~~~~~~~~~~
// Type instantiation is excessively deep and possibly infinite. (2589)
🙁 Actual behavior

I'm getting the "Type instantiation is excessively deep and possibly infinite. (2589)" error on what should be the identity.

🙂 Expected behavior

I'd expect this to just work. SpreadN should produce the same results as NTuple. It's surprising to me that this error is produced on the definition of SpreadN, rather than when it's instantiated with an overly large value of N.

I think this may have something to do with distribution over unions because when I reverse the conditional, the code works fine:

type NTuple2<T, N extends number, Acc extends T[] = []> =
  Acc['length'] extends N  // note that the check is reversed
  ? Acc
  : NTuple2<T, N, [...Acc, T]>

type SpreadN2<A extends number> = [...NTuple<any, A>]  // ok
type Pair2 = SpreadN2<2>;
//   ^? type Pair2 = [any, any]

It's not clear to me why reversing that conditional would matter, but it does have the effect of preventing distribution over unions, which is not desirable:

type T1 = NTuple<string, 2 | 3>;
//   ^? type T1 = [string, string] | [string, string, string]
type T2 = NTuple2<string, 2 | 3>;
//   ^? type T2 = [string, string]

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza reproduciendo el ejemplo de propagación de tuplas en el TypeScript Playground enlazado y compara NTuple con NTuple2, incluido su comportamiento con uniones. Se considera terminado cuando SpreadN ya no informa del error 2589 en su definición, mientras NTuple conserva los resultados esperados de uniones distribuidas.

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.