Skip to content

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

Closed
@chase-smith

Description

@chase-smith

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions