-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Limited type inference of types infered from tuple (bigger then one arity) when using function overloads. #18898
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
Order matters. Your more-specific overloads need to come first, because the overload resolution algorithm simply picks the first function that looks like it might works, then (incorrectly) applies a contextual type to the parameters before later undoing it. declare function tupleTest<T1, T2, T3, T4>(tuple: [T1, T2, T3, T4], func: (arg: T1, arg2: T2, arg3: T3, arg4: T4) => void): void;
declare function tupleTest<T1, T2, T3>(tuple: [T1, T2, T3], func: (arg: T1, arg2: T2, arg3: T3) => void): void;
declare function tupleTest<T1, T2>(tuple: [T1, T2], func: (arg: T1, arg2: T2) => void): void;
declare function tupleTest<T1>(tuple: [T1], func: (arg: T1) => void): void; I think @sandersn has a PR that fixes this, but in general the order should be the one above rather than the one you have |
Aaaa.. Thanks! Good to know. @RyanCavanaugh But it's still a bug right? Because it applies contextual typing from first overload but actually "uses" the later overload. I mean when I pressed |
It's a limitation of the checking algorithm - once we've fixed a type for a parameter, we can't "go back", so the first contextual application (even of an |
#5453 tracks implementing a better solution for variadic functions. |
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.5.3
I noticed that when we have function which has couple of overloads and where first argument use tuple then the types infered in tuple are not inferred properly in callback function that use this types.
Example:
Expected behavior:
Callback function in overload that use tuple of bigger arity than one should have it's type inferred.
Actual behavior:
For overload with bigger tuple arity than one types are inferred as
any
Strangely it happens only when function has overloads (like in above example). And only when I have overload that use tuple with one arity
After futher investigation it looks that there is a problem even when there is fuction overload without generic parameters:
Real world use case:
I found this issue when I was learning about angular HttpClient testing and wondered if
inject
function could be more type-safe and infer parameters of callback function. I came up with something like this:It works when you have signature with with 2-arity tuple but it breaks when there are other overloads.
(In this particular example from angular documentation you actually couldn't infer
HttpTestingController
because it's abstract class and it can't be assigned toType<T>
. I'm new to testing in angular but I guess if we could provide that nice type inference of tuples then maybe angular team would use not-abstract class or wouldInjectionToken<HttpTestingController>
or something like that (I see that non genericInjector.get
function is already deprecated so looks like they want to have stuff more type-safe and less "any
"))The text was updated successfully, but these errors were encountered: