microsoft / microsoft/TypeScript

Improve error message for incompatible signatures in union type from typed/untyped function call

Đang mở
#27,422 6 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Domain: Error Messages Experience Enhancement Suggestion
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

untyped function call error message union type incompatible signature

Example

This code errors as follows:

function foo<T>(x: T | (() => string)) {
    if (typeof x === "function") {
        let a = x();
             // ^ Cannot invoke an expression whose type lacks a call signature. 
             //Type '(() => string) | (T & Function)' has no compatible call signatures. [2349]
    }
}

which feels strange given that both of the following are fine:

function fooL<T>(x: T) {
    if (typeof x === "function") {
        let a = x();
    }
}

function fooR(x: () => string) {
    if (typeof x === "function") {
        let a = x();
    }
}

If an evaluation contexts accepts values of type A and values of type B, then it should accepts values of type A | B. I think what is going on is that the call signature from T & Function is untyped, which is incompatible with the typed signature of () => string.

I don't think this is very obvious to a user, and they end up seeing an application that works for both branches of a union, but not their composition.

Suggestion

I think making the example work is out of scope. My suggestion is to improve the error message, something like:

function foo<T>(x: T | (() => string)) {
    if (typeof x === "function") {
        let a = x();
             // ^ Cannot invoke an expression whose type lacks a call signature. 
             // Type '(() => string) | (T & Function)' has no compatible call signatures. [2349]
             //   Cannot combine untyped function call with typed function call
    }
}

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 chạy ví dụ TypeScript trong issue và tìm lỗi 2349 trong các chẩn đoán của compiler. Truy vết cách các chữ ký gọi của union tạo ra thông báo hiện tại. Công việc được xem là hoàn tất khi các lệnh gọi hàm có kiểu và không có kiểu không tương thích nhận được chẩn đoán giải thích rõ ràng hơn, đồng thời các ví dụ hiện có vẫn được bao phủ.

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ó
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
35/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.