microsoft / microsoft/TypeScript
Recursive tuple cannot be computed through generic, but valid in a declarative form
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
### 🔎 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
```ts
type Foo = {
leading: 'a',
rest: [Foo],
};
type Expand = T extends {
leading: infer L;
rest: infer R extends any[]
} ? [L, ...ExpandMany] : never;
type ExpandMany =
T extends [infer First, ...infer Rest] ? [Expand, ...ExpandMany] : [];
type TestA = Expand;
// ^~ Type instantiation is excessively deep and possibly infinite.(2589)
type TestB = ['a', TestB];
```
### 🙁 Actual behavior
`TestA` meant to be:
```
TestA
-> Expand
-> ['a', ...ExpandMany<[Foo]>]
-> ['a', ...[Expand]]
-> ['a', Expand]
-> ['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`, that is:
```
type TestA = Expand;
// ^?~ type TestA = ['a', Expand]
// 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`.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground example and confirm the TS2589 error for TestA while TestB is accepted. Trace how recursive conditional and tuple types are instantiated, then define completion as allowing the computed form to stabilize at a self-referential tuple without regressing recursion diagnostics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100