microsoft / microsoft/TypeScript
Why do we need to manually type unified overload signatures?
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ả
Please close if this has been asked before - nothing came up when searching.
Search Terms
overload, function, multiple, default, combine, unify, top
Suggestion
Today, when defining an overloaded call signature you have to manually type the implementation. Would it be possible to extend contextual types so TS can infer the implementation's types from context, just like TS already does for non-overloaded signatures?
Use Cases
Today, contextual typing isn't able to infer parameter types for overloaded call signatures. Instead, users have to manually type the implementation.
Examples
Before
type Reserve = {
(from: Date, to: Date, destination: string): Reservation
(from: Date, destination: string): Reservation
}
let reserve: Reserve = (
from: Date,
toOrDestination: Date | string,
destination?: string
) => { /* ... */ }
After
type Reserve = {
(from: Date, to: Date, destination: string): Reservation
(from: Date, destination: string): Reservation
}
let reserve: Reserve = (
from, // inferred as Date | Date = Date
toOrDestination, // inferred as Date | string
destination // inferred as string | undefined
) => { /* ... */ }
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
Bắt đầu bằng cách nghiên cứu kiểu hóa theo ngữ cảnh và hành vi của các chữ ký overload trong TypeScript, sử dụng các ví dụ trong issue làm tham chiếu về hành vi. Xác định cách các kiểu tham số nên được kết hợp giữa các overload và thêm coverage cho phép gán Reserve được minh họa. Hoàn thành khi các triển khai overload tương thích nhận được các kiểu tham số được suy luận mà không thay đổi đầu ra khi chạy.
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
- 25/100