microsoft / microsoft/TypeScript
type parameter variance in generic call signature is incorrectly bivariant
Chưa có ai nhận issue này.
- 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
unsound generic function assignability type parameter variance generic call signature bivariant
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics and variance.
⏯ Playground Link
💻 Code
type ShouldBeInvariant<D> = <T>(x: T&D, y: T)=>T&D
const second: ShouldBeInvariant<unknown> = (x, y) => y
const outNumber: ShouldBeInvariant<number> = second
outNumber<unknown>(4, 'str').toExponential()
const inNumber: ShouldBeInvariant<number> = (x) => (x.toExponential(), x)
const inUnknown: ShouldBeInvariant<unknown> = inNumber
inUnknown('str', 'str')
🙁 Actual behavior
The code type checks, while it shouldn't because that is unsound, using ShouldBeInvariant covariantly or contravariantly both causes runtime errors.
🙂 Expected behavior
ShouldBeInvariant should be invariant against its parameter. Both assigns:
const outNumber: ShouldBeInvariant<number> = second
and
const inUnknown: ShouldBeInvariant<unknown> = inNumber
should report errors
Additional information about the issue
No response
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với ví dụ TypeScript Playground được liên kết và tái hiện các phép gán không an toàn liên quan đến ShouldBeInvariant. Theo dõi tính có thể gán của chữ ký lời gọi generic và việc kiểm tra variance, sau đó thêm một bài kiểm thử hồi quy cho thấy cả hai phép gán đều báo lỗi và các lời gọi không hợp lệ không còn vượt qua bước kiểm tra kiểu.
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
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 48/100