microsoft / microsoft/TypeScript
Don't allow falsely discarded parameters when determining overloaded signature compatibility
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
Motivation
Libraries such as Knockout use overloaded functions, for example:
var x = new MyKnockoutModel();
console.log(x.customerName()); // string
x.customerName('bob'); // OK
x.customerName(42); // Error, 42 is not a string
We can model these in TypeScript:
interface KnockoutElement<T> {
// Getter
(): T;
// Setter
(value: T): void;
}
However, these overloads turn out to not be safe when used in callbacks. For example:
function readFile(finished: (contents: string) => void) { /*... */ }
var ko: KnockoutElement<number>;
readFile(ko); // No error, but passes a string to a number-expecting setter!
This is because we look at all the candidate source signatures, reject the (x :number) => void signature for having an incompatible type, and accept the () => T signature because it has fewer parameters (an acceptable variance).
This behavior does not map to any actual runtime behavior. The invoker's side might check the .length property of the function to determine what kind of arguments to pass, and the invokee's side might check arguments.length to determine which overload was intended, but the invoker cannot tell what the expected parameter type of the function is, and the invokee reasonably expects to be called with the correct type given a certain arity.
Proposal
As an example, if a callback declaration says it’s going to invoke f with two parameters, we should not consider overloads of f which take zero arguments if an overload with one or two arguments exists.
In 3.8.3 / 3.8.4:
M is a non-specialized call or construct signature and S’ contains a call or construct signature N where […]
Change to
M is a non-specialized call or construct signature and S’ contains a call or construct signature N where no other call or construct signature in S’ has more parameters than N but not more than M, and […]
Analysis
A sample implementation of this rule breaks no existing tests. Check stopParameterNeglect branch for code/tests.
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 đề xuất và kiểm tra branch stopParameterNeglect, bao gồm mã nguồn và các bài kiểm thử của branch này. Trước tiên, hãy chạy các bài kiểm thử hiện có, sau đó so sánh hành vi tương thích chữ ký với quy tắc đếm tham số được đề xuất. Công việc được xem là hoàn tất khi các overload chấp nhận ít tham số hơn không được chọn nếu có một overload tương thích chấp nhận nhiều tham số hơn trong phạm vi arity của callback, mà không làm hỏng các bài kiểm thử hiện 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
- Khá rõ ràng
- Mức phù hợp với người mới
- 48/100