microsoft/TypeScript

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

Opened this issue ยท 2 comments

๐Ÿ”Ž 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>.

๐Ÿค– Thank you for your issue! I've done some analysis to help get you started. This response is automatically generated; feel free to ๐Ÿ‘ or ๐Ÿ‘Ž this comment according to its usefulness.

Similar Issues

Here are the most similar issues I found

If your issue is a duplicate of one of these, feel free to close this issue. Otherwise, no action is needed.

Not sure what's going wrong here; the stack indicates a runaway instantiating ExpandMany<Rest>.

Alternative form that works the same:

type Expand<T> = T extends {
    leading: unknown;
    rest: any[]
} ? [T['leading'], ...T['rest']] : never;
type ExpandMany<T> = { [K in keyof T]: Expand<T[K]> };