Skip to content

Union type as argument to overloaded function #11353

Closed
@mchenzero

Description

@mchenzero

TypeScript Version: 2.0.3

Code

function fn(str: string): void;
function fn(num: number): void;
function fn(arg) {
  // do something with arg
}

function test(arg: string | number) {
  fn(arg); // from whatever source I get a (string | number)
}

Expected behavior: This code looks logically valid.

Actual behavior: It results in a compile error: error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'number'. Type 'string' is not assignable to type 'number'.

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