microsoft / microsoft/TypeScript

Recursive tuple cannot be computed through generic, but valid in a declarative form

Aperta
#62,704 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Type Circularity Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

🔎 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 R syntax not introduced
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.3#code/C4TwDgpgBAYg9nKBeKBvAUFKAbCBDAEwEsA7AcwC4oByPagGkygCcIBnYKgbXjgF1GAXwDc6dKEhQAogA8weEgQA8AFQB8yKCqgQZwCIrZomWXIVKUopAGYRmUADKisWVhyo27UAEo69BgiMFEC4+dEEoAH4oLgd6KAA6JNl5RQBZYKVvNT4oKhIIADc7UXFwaBSFAgySEFU-fUMoYNCNJBMtBoCjLk97GCJmDnikhL6fdmBc6K5KxSUBoeA1EeS5Kpq670mcvJi+UoloFUmAQU055V41UQB6W5dHx4A9AD8tcqsSDgVgIjw-nASFYjLoAMbsNhEYrYEBQAgQCBgZqKKBgOBsKEAI1hX2spCI+gSAAoAEwAVgAHABOACUYiOWkmACFNFxaAwmRxmQd0EA

💻 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>.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l’esempio collegato di TypeScript Playground e conferma l’errore TS2589 per TestA, mentre TestB viene accettato. Traccia come vengono istanziati i tipi condizionali e tupla ricorsivi, quindi definisci il completamento in modo da consentire alla forma calcolata di stabilizzarsi su una tupla autoreferenziale senza regressioni nella diagnostica della ricorsione.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.