microsoft / microsoft/TypeScript

Why do we need to manually type unified overload signatures?

Đang mở
#25,352 3 bình luận 13 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ả

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

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

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

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.