Skip to content

Misleading compiler error when calling overloaded function with incorrect params #20697

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

Closed
UselessPickles opened this issue Dec 14, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@UselessPickles
Copy link

TypeScript Version: 2.6.2

Code
(with all strict compiler options enabled)

declare function foo(value: number, other: number): void;
declare function foo(value: number | null, other: string): void;

declare const x: number;

foo(x, true);

Expected behavior:
A compiler error complaining that the second arg (type boolean) is not assignable to type number, using the first overloaded signature.

I would expect the compiler error to be based on the "most applicable" overload signature. If multiple signatures are equally "applicable", then the order of the signatures should be the tie breaker.

Actual behavior:
A compiler error complaining that the second arg cannot be assigned to type string, using the last overloaded signature. When none of the overload signatures are valid, the compiler seems to always use the last overload signature for creating the error message. This can be very misleading.

screen shot 2017-12-14 at 11 40 46 am

In this situation, it is more likely that the developer should be guided toward providing a number as the second arg, because the type of their x variable does not require the second overload signature to be used.

@RyanCavanaugh
Copy link
Member

See #20139 (comment) and many other issues suggesting that we pick the "best" overload

@UselessPickles
Copy link
Author

Can't you just program the compiler to read my mind? :)

I'm sure there's many layers of complexity I'm not aware of, but this is my initial thought on selecting the "best" overload for errors:

  • Pick the overload with the highest number of params that are assignable (maybe include +1 in the count if return value is assignable?).
  • Use the order of the overrides as a tie-breaker (pick the first overload with the highest number of assignable params).

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants