Repository navigation
Improper type inferred in presence of overloads #10957
Copy link
Copy link
Closed
Labels
QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
Description
Activity
This example from Rado Kirov (@rkirov) is a bit smaller and has maybe the same error.
interface Editor { on(h: (x: boolean) => void): void; on(h: (x: string, e: string) => void): void; } let cm: Editor = null as any; cm.on((x, y) => {});
Argument of type '(x: boolean, y: any) => void' is not assignable to parameter of type '(x: string, e: string) => void'. Types of parameters 'x' and 'x' are incompatible. Type 'string' is not assignable to type 'boolean'.- addedNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.and removedNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.
on May 24, 2017 I ran into this and I found out that if you replace the overload by an equivalent Union type it works! Though an overloaded function seems to me an intersection type but that doesn't work.
This is maybe not so hard to fix so I hope the typescript team can give this some prio.
Faulty behavior in the case of overloads:
interface MyFunction1 { (): string (): number } // should be string|number but actually is number type StringOrNumber1 = ReturnType<MyFunction1>
Correct behaviour in the case of Union types:
type MyFunction2 = (() => string) | (() => number) // is string|number type StringOrNumber2 = ReturnType<MyFunction2>
- addedQuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in codeand removedNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.
on Aug 23, 2019 RyanCavanaugh commented
on Aug 23, 2019 MemberMore actionsThe definition in the OP has signatures in the wrong order - callbacks with more parameters should go first:
interface Editor { on(e: string, h: (x: string, e: any) => void): void; on(e: string, h: (x: Editor) => void): void; on(e: 'foo', h: (x: Editor, e: any) => void): void; }
- locked as resolved and limited conversation to collaborators
on Oct 21, 2025
Metadata
Metadata
Assignees
Labels
QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
TypeScript Version: 2.1.0-dev.20160916
Code
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:
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.)