Skip to content

Typescript 3.4 breaks some type inference with method overloads. (unknown) #36735

@garronej

Description

@garronej

TypeScript Version: 3.4 and up

Search Terms:
unknown, 3.4, type inference fails, method overload

Code

type Circle = {
    type: "CIRCLE";
    radius: number;
};

type Square = {
    type: "SQUARE";
    sideLength: number;
};

type Shape = Circle | Square;


class Evt<T> {

    public attach<U>(
        matcher: (data: T)=> [U] | undefined,
        callback: (transformedData: U)=> void
    ): Promise<U>;
    public attach(
        matcher: (data: T) => boolean,
        callback: (data: T) => void
    ): Promise<T>;
    public attach(...args: any[]){
        return null as any;
    }

}

const evtShape = new Evt<Shape>();

//OK
const prRadius = evtShape.attach(
    shape => shape.type === "CIRCLE" ? [shape.radius] : undefined,
    radius => { }
);


const prShape = evtShape.attach(
    shape => true,
    shape => { } //<====== In typescript 3.4 and up shape is unknown 
);

Expected behavior:

In the last line, shape should be of type Shape. In typescript 3.4 and up it is of type unknown.

Actual behavior:

Playground Link

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions