-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improper type inferred in presence of overloads #10957
Comments
This example from @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) => {});
|
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> |
The 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;
} |
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.)
The text was updated successfully, but these errors were encountered: