Skip to content

Error in type in overloaded function #39962

Closed
@whpac

Description

@whpac

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.

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