microsoft / microsoft/TypeScript

strictFunctionTypes prevents an assignment not related to functions

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

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

Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

TypeScript Version: 3.3.0-dev.20181122

Search Terms:
covariance, contravariance, keyof, interface, class, generic

Code

interface IBase {
    foo: string;
}

interface IDerived extends IBase {
    bar: string;
}

type StringPropertyNames<T> = { [P in keyof T]: T[P] extends string ? P : never }[keyof T]
type StringProperties<T> = Pick<T, StringPropertyNames<T>>

interface Foo<T> {
    readonly Bar: StringProperties<T>;
}

let baseProperties: StringProperties<IBase>
let derivedProperties: StringProperties<IDerived>

let baseInterface: Foo<IBase>
let derivedInterface: Foo<IDerived>

baseProperties = derivedProperties // no error
baseInterface = derivedInterface

tsc test.ts --strictFunctionTypes

Expected behavior:
Compiles without errors.

Actual behavior:

test.ts:23:1 - error TS2322: Type 'Foo<IDerived>' is not assignable to type 'Foo<IBase>'.
  Property 'bar' is missing in type 'IBase' but required in type 'IDerived'.

23 baseInterface = derivedInterface
   ~~~~~~~~~~~~~

  test.ts:6:5
    6     bar: string;
          ~~~
    'bar' is declared here.

Playground Link:

Related Issues:
#24190

The documentation says:

Under --strictFunctionTypes function type parameter positions are checked contravariantly instead of bivariantly.

I couldn't find any information saying it should affect anything else.

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

Tái hiện ví dụ được cung cấp trong test.ts bằng tsc test.ts --strictFunctionTypes, rồi so sánh hai kết quả phép gán. Payload không nêu tên tệp triển khai hoặc bài kiểm thử hồi quy nào, vì vậy hãy lần theo đường đi của quá trình kiểm tra kiểu đối với phép gán kiểu ánh xạ generic được báo cáo. Hoàn thành khi hành vi phép gán được báo cáo khớp với kết quả mong đợi mà không gây hồi quy trong việc kiểm tra nghiêm ngặt tham số hàm.

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ó
5/5
Thời gian dự kiến
Hơn một tuần
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
28/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.