Skip to content

Function type union works unexpectedly #12404

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
ethanresnick opened this issue Nov 21, 2016 · 5 comments
Closed

Function type union works unexpectedly #12404

ethanresnick opened this issue Nov 21, 2016 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@ethanresnick
Copy link
Contributor

ethanresnick commented Nov 21, 2016

TypeScript Version: nightly (2.2.0-dev.20161121)

Code

declare const fn: (((...args: any[]) => any) | (() => boolean));

fn(1, 2, 3);

Expected behavior:
The call to fn(1, 2, 3) should succeed, as it should match the (...args: any[]) => any part of the union.

Actual behavior:

I get the error: "Supplied parameters do not match any signature of call target."

@alitaheri
Copy link

This is Working as Intended. The operator => takes precedence over | so:

type A = (...args: any[]) => any | (() => boolean);

Is equivalent to:

type A = (...args: any[]) => (any | (() => boolean));

Not:

type A = ((...args: any[]) => any) | (() => boolean);

Try:

type AnyFn = (...args: any[]) => any;
declare const fn: (((...args: any[]) => any) | (() => boolean));
declare const fn2: (AnyFn | (() => boolean));

fn(1, 2, 3);
fn2(1, 2, 3);

I tested it, works fine.

Be more careful with operator precedence, it bites bad 🙀

@ethanresnick
Copy link
Contributor Author

@alitaheri Thanks. Good call on the precedence, and you're right that with your revised code the two declarations behave equivalently. However, now both function calls give an error! So I guess the issue is with how the two types are being unioned? I'll update the OP accordingly.

@ethanresnick ethanresnick changed the title Pulling type into alias changes behavior Function type union works unexpectedly Nov 21, 2016
@thorn0
Copy link

thorn0 commented Nov 21, 2016

Duplicate of #7294 ?

@ethanresnick
Copy link
Contributor Author

Seems like it might be, though I'll wait for @RyanCavanaugh to confirm before closing this.

@ethanresnick
Copy link
Contributor Author

Looking at #7294 more closely, they're definitely the same problem conceptually. I'm gonna give this a close.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 21, 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

4 participants