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

Đang mở
#63,207 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Domain: check: Type Circularity
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách cài đặt remeda và chạy bản tái hiện TypeScript độc lập với tsserver. So sánh trường hợp kiểu trả về được suy luận bị treo với chú thích Pick tường minh, sau đó lần theo cách xử lý tính vòng quanh chuỗi PickFromArray, IsBoundedRecord, IsBounded, KeysOfUnion và UnionToIntersection đã được báo cáo. Hoàn tất khi tsserver không còn bị treo và либо báo lỗi kiểu trả về vòng, либо chuyển sang any kèm cảnh báo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.