Skip to content

Overload with type unions #27131

@domoritz

Description

@domoritz

TypeScript Version: 3.1.0-dev.201xxxxx

Search Terms:

overload, type union

Code

function f(a: number): number;
function f(a: string): string;
function f(a: string | number): string | number { 
  return a;
}

function foo(value: string | number) {
  f(value)
}

Expected behavior:

Should be valid.

Actual behavior:

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

Playground Link: https://www.typescriptlang.org/play/#src=function%20f(a%3A%20number)%3A%20number%3B%0D%0Afunction%20f(a%3A%20string)%3A%20string%3B%0D%0Afunction%20f(a%3A%20string%20%7C%20number)%3A%20string%20%7C%20number%20%7B%20%0D%0A%20%20return%20a%3B%0D%0A%7D%0D%0A%0D%0Afunction%20foo(value%3A%20string%20%7C%20number)%20%7B%0D%0A%20%20f(value)%0D%0A%7D%0D%0A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions