microsoft / microsoft/TypeScript
Improve typing of arguments with a function (with respect to overloads)
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
function overload arguments narrowing
Suggestion
The example is probably the best illustration I can give... It's also much clearer (I think) that the description in text.
Currently, the types of the arguments of a function with overloads cannot be forwarded to another function with the same overloads (1) because the raw typing of the argument doesn't match any of the overloads. But, at the same time, this restriction is not carried out in the relationship between the arguments' types is not used once in the function's body.
We currently have to either assert types (using as) or do if (...) throw with conditions that will never be met runtime.
That requires to sacrifice either type safety or performance.
I've seen #13225. However, my suggestion is more regarding the arguments than the return type. It shouldn't impose any additional restrictions that currently exist (actually, remove some).
Moreover, one of the arguments that was given at the time ("Pass-through overloads are extremely common and would require writing "artificial" code to satisfy the checker") is not true anymore. (See point (1) in the example). I'm not sure if this particular point should be considered a bug in itself or not...
Having the type-system aware of the overloads when typing the arguments could actually restore this.
Use Cases
Simplify overloads implementation and make them more readable.
Examples
let baz: string;
function fn2(foo: 'a', bar: string): void;
function fn2(foo: 'b', bar: number): void;
function fn2(foo: 'a' | 'b', bar: string | number): void
{
fn(foo, bar); // (1) If this doesn't work, ...
}
function fn(foo: 'a', bar: string): void;
function fn(foo: 'b', bar: number): void;
function fn(foo: 'a' | 'b', bar: string | number): void
{
if (foo == 'a')
{
baz = bar; // (2) ... This should!
}
}
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript / JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. new expression-level syntax)
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
Issue cung cấp một ví dụ playground TypeScript nhưng không nêu tên tệp hay bài kiểm thử nào trong repository. Hãy bắt đầu bằng cách tái hiện triển khai overload và forwarding call, sau đó điều tra hành vi của checker đối với các đối số overload; hoàn thành có nghĩa là forwarding call được chấp nhận và nhánh điều kiện thu hẹp bar thành string mà không cần assertions hoặc các kiểm tra runtime không thể đạt tới.
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
- Tính năng
- Độ 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
- 35/100