microsoft / microsoft/TypeScript

type parameter variance in generic call signature is incorrectly bivariant

Đang mở
#63,677 2 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: Variance Relationships
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

https://www.typescriptlang.org/play/?ts=6.0.3#code/C4TwDgpgBAygFgewK4BsAmAhCBJAdgNwEMAnAS0N2AB4ARAPigF4oqAVOgCgA8AuKVgGQ0ANFBB9WASkZ1BNAFDyAxglwBnYFDUQVuNH3jJ0WPETIVqSXAGtcCAO64GzKN1EhJTBiGWqNUZGAAOSQAWwAjCGIDRFRMHAISckoqXDDI4mctHVU0eUCQiKiqK1sHJw4AFlEAcg1iGskAOmAEAFEuMFUISnIUDklFXX9SXEKMmKN40ySLVPSorO5PGVcuFvbO7t7CfslRLkHhzVGAVRs7R0m4k0TzFNLLpyYoUfGo+TOL8o464AbavVGoogA

💻 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

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

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.