microsoft / microsoft/TypeScript
tsserver hangs indefinitely when inferring return type of class method that passes this to a function with deeply nested conditional return type
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
### 🔎 Search Terms
hang circular
### 🕗 Version & Regression Information
This behavior is present in TypeScript 5 and TypeScript 6 beta.
### ⏯ Playground Link
N/A — requires the remeda package (npm i remeda). Reproduction is a self-contained file below.
### 💻 Code
```ts
import { pick } from "remeda";
export class MyClass {
constructor(public readonly field: number) {}
// [bad] tsserver hangs indefinitely: no explicit return type
getIdentity() {
return pick(this, ["field"]);
}
// [good] works fine: explicit return type breaks the cycle
getIdentityFixed(): Pick {
return pick(this, ["field"]);
}
}
```
### 🙁 Actual behavior
`tsserver` becomes completely unresponsive. The editor (VS Code) loses all language service features (hover types, completions, diagnostics) until tsserver is manually restarted. No error is emitted; tsserver simply loops forever.
The cause is an undetected circular type inference chain. To infer the return type of `getIdentity()`, TypeScript must evaluate `PickFromArray` (remeda's `pick` return type). That involves `IsBoundedRecord` → `IsBounded>`. `KeysOfUnion` uses `UnionToIntersection`, which places `MyClass` in a contravariant function parameter position — forcing eager, non-deferred evaluation of the full structural type of `MyClass`. The full type of `MyClass` includes `getIdentity: () => `, whose inferred return type is what we started trying to compute. Because the cycle passes through ~5 distinct type alias instantiations, TypeScript's cycle-detection heuristics never fire, and the checker loops indefinitely instead of emitting a "circularly references itself" error.
### 🙂 Expected behavior
TypeScript should detect the circular inference and either:
- Emit a "Return type annotation circularly references itself" error (as it does for simpler cycles), or
- Fall back to any with a warning.
Either outcome is acceptable. The checker should never hang.
### Additional information about the issue
Workaround: add an explicit return type annotation to the method. This gives TypeScript the type of `MyClass.getIdentity()` upfront, preventing the cycle from forming.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず remeda をインストールし、tsserver で自己完結型の TypeScript 再現コードを実行します。ハングする推論戻り値のケースと、明示的な Pick アノテーションを比較し、その後、報告されている PickFromArray、IsBoundedRecord、IsBounded、KeysOfUnion、UnionToIntersection の連鎖における循環性の処理を追跡します。tsserver がハングしなくなり、循環戻り値型エラーを報告するか、警告付きで any にフォールバックすれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100