Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
4 tasks done
jack-williams opened this issue Sep 28, 2018 · 6 comments
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@jack-williams
Copy link
Collaborator

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)
@jack-williams jack-williams changed the title Improve error message for incompatible signatures in union type from type/untyped function call Improve error message for incompatible signatures in union type from typed/untyped function call Sep 28, 2018
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Experience Enhancement Noncontroversial enhancements labels Oct 1, 2018
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Oct 1, 2018
@jack-williams
Copy link
Collaborator Author

Is this approved? Shall I pick it up?

@weswigham weswigham added Domain: Error Messages The issue relates to error messaging and removed Suggestion An idea for TypeScript labels Nov 6, 2018
@RyanCavanaugh RyanCavanaugh added the Suggestion An idea for TypeScript label Mar 7, 2019
@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@DanielRosenwasser
Copy link
Member

I think if you're interested in tackling this, it'd be reasonable.

@jack-williams
Copy link
Collaborator Author

@DanielRosenwasser Sure!

@jack-williams
Copy link
Collaborator Author

Before I start work on this. Is there a reason why the union cannot be combined into an untyped call signature?

@BeanHeaDza
Copy link

Here is another simple example:

const test: string[] | number[] = []
const test1 = test.map(x => console.log(x));

the error message:

Cannot invoke an expression whose type lacks a call signature. Type '((callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | ((callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => U[])' has no compatible call signatures.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented May 30, 2019

@jack-williams not sure what you have in mind there. oh because of the Function type. I can't weigh in on that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants