microsoft / microsoft/TypeScript

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

未关闭
#62,704 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Domain: check: Type Circularity Help Wanted
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 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`.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的 TypeScript Playground 示例开始,确认 TestA 出现 TS2589 错误,而 TestB 会被接受。跟踪递归条件类型和元组类型的实例化过程,然后将完成定义为:允许计算形式在自引用元组上稳定下来,同时不使递归诊断发生回归。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。