Skip to content

Improper type inferred in presence of overloads #10957

@evmar

Description

@evmar

TypeScript Version: 2.1.0-dev.20160916

Code

interface Editor {
  on(e: string, h: (x: Editor) => void): void;
  on(e: string, h: (x: string, e: any) => void): void;
  on(e: 'foo', h: (x: Editor, e: any) => void): void;
}

let cm: Editor = null as any;
cm.on('bar', (x, y) => {});

Expected behavior:

cm.on() infers some type for its second param that is accepted by the compiler, given that the param has no types specified.

Actual behavior:

err.ts(8,14): error TS2345: Argument of type '(x: Editor, y: any) => void' is not assignable to parameter of type '(x: string, e: any) => void'.
  Types of parameters 'x' and 'x' are incompatible.
    Type 'string' is not assignable to type 'Editor'.

It appears to infer that its param should have the third overload's expected type, but then typechecks it against the second overload.

It appears to also be sensitive to the order in which the overloads are declared.

(This is reduced from a larger example that involves the CodeMirror d.ts files.)

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