microsoft / microsoft/TypeScript

Parameters in functions with type parameters are not contextually-typed

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

@ahejlsberg đang làm issue này rồi.

Từ ngày 4/6/2025.

Needs Investigation
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

type parameters generic conextual parameter types context sensitive

### 🕗 Version & Regression Information

- This is the behavior in every version I tried

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250601#code/KYDwDg9gTgLgBAE2AYwDYEMrDsiA7AZ3gDM8AuOAHgBUAaOAQSgHMC5QZg8E2mp0AnpXR4BAPnoAlYDDEAKALAAoOHALBUxCnWWqARhAQCKcmAAsAlgW30AdPcysKTVgEo4AXjFwA4l2D8MNDColIy9HjAAG4BYsruXnBy9raO1owsBAne0jDKyqCQsDj4RHAQANaecKRyAN5wALbABATozMAUAETEEBBdcAC+9MQArnjIMBb4AFRy5HB4o416Ae51uiWEEKjAtqgQzKaWBLbNre3AEa7KgzdKBeDQ8LiE8HgQ1bUN520d3b1+kMRuNJtM8DNKAA5eQLKHrTavAg7PYHI7mKxnFp-K6Le53ZRAA

### 💻 Code

```ts
export declare const fn: , Ret>(
self: T,
body: (this: T, ...args: Args) => Generator
) => (...args: Args) => Ret

export const ok = fn({ message: "foo" }, function*(n: number) {
console.log(this.message, n) // ok
})

export const no = fn({ message: "foo" }, function*(n: N) {
console.log(this.message, n) // error
})
```

### 🙁 Actual behavior

The second case errors

### 🙂 Expected behavior

Both of those should work fine

### Additional information about the issue

Given TS is able (at times) to infer new generic signatures based on generic function arguments (notice the return type of the failing case: `const no: (n: N) => void`), it makes sense to assign contextual parameter types.

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.

Đánh giá

Issue này chưa được đánh giá.

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.