-
Notifications
You must be signed in to change notification settings - Fork 12.8k
loss of type safety when using function overloads with callbacks #13430
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
Comments
declare function it(f: (x: string) => void): string;
declare function it(f: () => void): number; The more specific overload needs to be declared first. |
@aluanhaddad note the return type of |
I'm not sure what you mean, the correct overload is chosen if ordered as shown declare function it(f: (x: string) => void): string;
declare function it(f: () => void): number;
let r = it((x) => {x}); // string
I believe must have been the result of a bug being fixed as this behavior is intentional, see #10645 (comment). |
I meant is that if you go - and hover over I find that surprising, as if TS choose the first overload for the callback body, but the second for the return type. But maybe my mental model is off. |
This should be now flagged as an implicit any under |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.1.4
Code
Expected behavior:
The inferred type of
x
inside the closure should bestring
.Actual behavior:
The inferred type of
x
isany
. 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
isstring
thus the second overload was ultimately chosen. Despite that, the type ofx
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
The text was updated successfully, but these errors were encountered: