microsoft / microsoft/TypeScript
Member-wise checks give incorrect errors for type assertions
Chưa có ai nhận issue này.
- 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ả
// No error. ✅
let okay = { id: 123, blah: "extra" } as { id: number };
// Errors. ❌
let waat = [{ id: 123, blah: "extra" }] as { id: number }[];
// ~~~~
// Conversion of type '{ id: number; blah: string; }[]' to type '{ id: number; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
// Type '{ id: number; blah: string; }' is not comparable to type '{ id: number; }'.
// Object literal may only specify known properties, and 'blah' does not exist in type '{ id: number; }'.
Expected: Both of these type assertions should consistently apply the appropriate relationship check, and both should be free of errors.
Actual: The first assertion is free of errors, while the second has an excess property error.
Similar example I found over at https://github.com/microsoft/TypeScript/pull/55152/files#r1275497478:
let okay = { foo: "" } as { id: 123 };
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
// Conversion of type '{ foo: string; }' to type '{ id: 123; }' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
// Property 'id' is missing in type '{ foo: string; }' but required in type '{ id: 123; }'.
let waat = [{ foo: "" }] as { id: 123 }[];
// ~~~
// Conversion of type '{ foo: string; }[]' to type '{ id: 123; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
// Type '{ foo: string; }' is not comparable to type '{ id: 123; }'.
// Object literal may only specify known properties, and 'foo' does not exist in type '{ id: 123; }'.
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 bản tái hiện TypeScript Playground được liên kết và so sánh hai phép khẳng định kiểu, bao gồm ví dụ tương tự trong PR #55152. Truy vết việc kiểm tra mối quan hệ theo từng member được sử dụng cho các phép khẳng định array và thêm một bài kiểm thử hồi quy cho các trường hợp đã báo cáo; được xem là hoàn tất khi cả hai phép khẳng định đều nhất quán tránh lỗi excess-property không chính xác.
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
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 45/100