microsoft / microsoft/TypeScript

Covariant generic parameters fail unification

Đang mở
#61,503 0 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 Inference Help Wanted
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

"contravariant", "unification"

### 🕗 Version & Regression Information

- This changed between versions 3.3.3 and 3.5.1

### ⏯ Playground Link

https://www.typescriptlang.org/play/?jsx=0#code/MYewdgzgLgBKZQE4EMBuzEEtkJgXhgAoBKfAPhgG8AoGOcaegWwCNMwBTfGAHgGEoADzKEA+gEYAXEWCSBg0ngpgArgBs1AGhiiATNMKz5i5erXEDJmPPJErKsABMOAM3YdHMZBC9gAngDc1LT0kLBqHBAQovDQWJyeBPwwHIJQHE4+lBFgAOZQABaSqqwciAC+IrLWFjCqGrb1akF0sbBMIIgcMQxI7om8fClpGY5ZOflFJSxl2sAFGHwgzgCCUGLFKqWIFtNllYbSfLVNjWYtoYzp0OLcoKzuhBFRPWHxHtodXa9x-cQXbRg1ygujuIAenEIX26bXejm0z2isL+AN6MEQ3GB4hIQXKwUBoHQWBwsAIJFsNFaaPubE43H4QhEEkstnk2j0LKU1iEtRsXIczjcCS8PhwgWCVLCMERPz6wqSQ1S6UyVAmhU22wOvNsAtc-RFvnFksY0NlcPpipGKuyGUmGpmiDmC0QS1W61E9rKuy2Dq1Rx1Tj1wu8htRUqxYIhHCekSRvThn06MPjKJCgOBoIINMepuRCQRsbNqfKQA

### 💻 Code

```ts
const contravariant = () => {
const combine = (_1: (c:Ctx) => null, _2: (c:Ctx) => null): () => Ctx => () => undefined as any;

const less_constrined = (c: C): null => null;
const more_constrined = (c: C): null => null;
const test1 = combine(less_constrined, more_constrined);
const test2 = combine(more_constrined, less_constrined);
}

const covariant = () => {
const combine = (_1: () => Ctx, _2: () => Ctx): Ctx => undefined as any;

const less_constrined = (): C => undefined as any;
const more_constrined = (): C => undefined as any;
const test1 = combine(less_constrined, more_constrined);
const test2 = combine(more_constrined, less_constrined);
}
```

### 🙁 Actual behavior

In the `contravariant` example the type is inferred from the first argument, so `test1` fails to typecheck, despite both `test1` and `test2` sharing the same argument types, just reversed. This is also different from the `covariant` case where types are properly unified in both `test1` and `test2`

### 🙂 Expected behavior

The behaviour for the covariant and contravariant case should match, so `test1` should be inferred as
`
() => C
`

### Additional information about the issue

_No response_

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 repro TypeScript Playground được liên kết và so sánh các lệnh gọi phản biến với các lệnh gọi đồng biến, bao gồm hành vi được báo cáo giữa các phiên bản 3.3.3 và 3.5.1. Issue được hoàn tất khi cả hai thứ tự đối số đều được hợp nhất một cách nhất quán và lệnh gọi phản biến đầu tiên suy luận kiểu C bị ràng buộc chặt hơn được nêu trong hành vi mong đợi.

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
Đì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.