Skip to content

loss of type safety when using function overloads with callbacks #13430

Closed
@rkirov

Description

@rkirov

TypeScript Version: 2.1.4
Code

declare function it(f: () => void): number;
declare function it(f: (x: string) => void): string;

let r = it((x) => {x});

Expected behavior:
The inferred type of x inside the closure should be string.

Actual behavior:
The inferred type of x is any. Moreover, this triggers noImplicitAny errors.

It appears that the first overload somehow throws off the type inference (no issue without overloads). However, the resulting type of r is string thus the second overload was ultimately chosen. Despite that, the type of x is not inferred to match the one in the overload.

This example worked as expected in TS 2.0.x so it appears to be a regression.

//cc @calebegg

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions