microsoft / microsoft/TypeScript

Generic recursive function infers incorrect return type

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

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

Bug
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

generic, recursive, return, infer

### 🕗 Version & Regression Information

- This changed between versions 5.4.5 and 5.5.4
- This bug continues to exist in nightly

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3#code/MYewdgzgLgBAZjAvDAPAFQDQD4AUBDALhkxgAciwBXAWwCMBTAJwEoksYBvAKBl5gEsEOAISlWjelEqMwMPAG4efCVJnwcpDGRgBaGAEZmXAL5cuoSLAAeSdQCI4IEHa0BWIxYggANvQB03iAA5jhWzEA

### 💻 Code

```ts
const f = (a: T, p: number) => {
if (!p) return a;
return f(p, p - 1) // f
}

const x = f("foo", 5) // inferred type "foo"
console.log(x) // 1
```

### 🙁 Actual behavior

The compiler infers `f`'s return type to be `T`. Then `x` is inferred to have type `"foo"` (or `string`), even though the actual return value is `1` (type `number`).

### 🙂 Expected behavior

In the example `f` is a recursive and generic function. Importantly, when recursing `f` calls itself on a _non-generic_ type.

If `p` is falsy then `f` returns `a`, which has the generic type `T`. This is the "base case". However if `p` is truthy then `f` calls itself with type parameter `number`, so in all the recursive cases the return type is actually `number`.

So the actual return type of `f` should be `T | number`.

### Additional information about the issue

In earlier versions of the compiler (I tested 5.4.5) recursive functions without explicit return type annotations are flagged as an error, so we sidestep this bug by requiring the return type to be manually declared:

> 'f' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions. (7023)

In later versions however the compiler seems to think it can infer the return type, leading to this incorrect inferred result. Interestingly even though the compiler infers `f` to return type `T`, if we add the explicit return type annotation `T` then the compiler actually becomes unhappy (geez, make up your mind):

```ts
const f = (a: T, p: number): T => {
if (!p) return a;
return f(p, p - 1) // Type 'number' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to 'number'. (2322)
}
```

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 với bản tái hiện được liên kết trong TypeScript Playground và so sánh hành vi suy luận giữa các phiên bản trình biên dịch được báo cáo. Theo dõi cách các kiểu trả về generic đệ quy được suy luận khi lời gọi đệ quy sử dụng một kiểu cụ thể, sau đó thêm một bài kiểm thử hồi quy cho ví dụ này. Được xem là hoàn tất khi hàm được suy luận là trả về T | number và kết quả suy luận không chính xác không còn được chấp nhận.

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ó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
52/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.