Skip to content

Signature overloading for arguments of type function fails #12797

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
CaselIT opened this issue Dec 9, 2016 · 2 comments
Closed

Signature overloading for arguments of type function fails #12797

CaselIT opened this issue Dec 9, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@CaselIT
Copy link

CaselIT commented Dec 9, 2016

TypeScript Version: 2.1.4
In typescript 2.1.4 the compiler fails to narrow the correct signature overloading of a function when a arguments is a function which can optionally take an argument.
Code

function test(fn: () => void): void;
function test(fn: (x: number) => void): void;
function test(fn: (x?: number) => void) {
  fn(1);
};

test((x) => { });

Expected behavior:
Compile without errors, like previous version of typescript.
Actual behavior:
When compiling with --noImplicitAny I get the error error TS7006: Parameter 'x' implicitly has an 'any' type.

Also VSCode selects the correct overload:
image
but the type of x is any:
image

@RyanCavanaugh
Copy link
Member

Duplicate #11936

The proper declaration for test looks like this

function test(fn: (x: number) => void): void;
function test(fn: () => void): void;
function test(fn: (x?: number) => void) {
  fn(1);
};

@CaselIT
Copy link
Author

CaselIT commented Dec 12, 2016

Sorry, I did not find that issue before opening this.

It's strange that the order of the overloads changes the compiler resolution. Still reading the other issue it seems that also before this example was not working correctly.

I'll close this.

@CaselIT CaselIT closed this as completed Dec 12, 2016
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 12, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

2 participants