Skip to content

Function with overloads that uses a union type parameter in implementation function throws error #13041

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
chase-smith opened this issue Dec 20, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@chase-smith
Copy link

TypeScript Version: both 2.1.4 and nightly (2.2.0-dev.20161219)

Code

function func(foo: string): number;
function func(foo: number): number;
function func(foo: any): number {
    return 0;
}

function func2(bar: number): number;
function func2(bar: string): number;
function func2(bar: any): number {
    return func(bar); // no problem here
}

function func3(bar: string): number;
function func3(bar: number): number;
function func3(bar: string | number): number {
    let v = func(bar); // error here
    return v;
}

Expected behavior:
There should be no issue with the call in func3.
Actual behavior:

error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'.
  Type 'string' is not assignable to type 'number'.

This seems to be in direct contradiction to what the TS GitHub wiki says about function overloading, and the impact of the shape of the implementation function on the allowed types of the function:
image

@RyanCavanaugh
Copy link
Member

Duplicate #1805

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