Skip to content

Overloading function fails with union types #29969

Closed
@burtek

Description

@burtek
// @strictNullChecks: true

interface Data {
    data: any
}

declare const value: Data | undefined;

function doSthWithValue(v: undefined): undefined;
function doSthWithValue(v: Data): Data;
function doSthWithValue(v?: Data) {
    return v;
}

const a = doSthWithValue(value); // <-- error

EDIT: see comment below, it is not connected to strictNullChecks

Expected behavior:

Should not throw error with strictNullChecks enabled

Actual behavior:

It throws error:

Argument of type 'Data | undefined' is not assignable to parameter of type 'undefined'.
  Type 'Data' is not assignable to type 'undefined'.

Playground Link:

Enable strictNullChecks. The order of overloaded signatures does not change anything

https://www.typescriptlang.org/play/#src=interface%20Data%20%7B%0D%0A%20%20%20%20data%3A%20any%0D%0A%7D%0D%0A%0D%0Adeclare%20const%20value%3A%20Data%20%7C%20undefined%3B%0D%0A%0D%0Afunction%20doSthWithValue(v%3A%20undefined)%3A%20undefined%3B%0D%0Afunction%20doSthWithValue(v%3A%20Data)%3A%20Data%3B%0D%0Afunction%20doSthWithValue(v%3F%3A%20Data)%20%7B%0D%0A%20%20%20%20return%20v%3B%0D%0A%7D%0D%0A%0D%0Aconst%20a%20%3D%20doSthWithValue(value)%3B%0D%0A

Related issues:
#14107

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