Skip to content

Error in type in overloaded function #39962

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
whpac opened this issue Aug 8, 2020 · 2 comments
Closed

Error in type in overloaded function #39962

whpac opened this issue Aug 8, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@whpac
Copy link

whpac commented Aug 8, 2020

TypeScript Version: 3.9.6 (tested also with 4.0.0-beta and Nightly in TS Playground and still present)

Search Terms: overload

Code

function foo(x: number): object;
function foo(x: number[]): object[];
function foo(x: number | number[]): object | object[]{
    // Do something
    return typeof x == 'number' ? {} : [{}];
}

function bar(x: number): object;
function bar(x: number[]): object[];
function bar(x: number | number[]): object | object[]{
    return foo(x); // Error...
}

Expected behavior: Compiles without an error

Actual behavior: Compiler throws an error: No overload matches this call (1) number | number[] is not assignable to number, (2) number | number[] is not assignable to number[]

Playground Link: Here

Related Issues: I haven't found any

Edit: I've tweaked the code in order to fit the type declaration. This change has no impact on the actual bug I'm reporting.

@MartinJohns
Copy link
Contributor

Duplicate of #39885. Search terms used: overload union

@orta orta added the Duplicate An existing issue was already created label Aug 10, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants