You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Infer type in conditional cannot unify generics #22617
Repeating my comments from #22615 as they apply equally here:
It is effectively a design limitation. We have the concept of instantiating a generic function type in the context of a non-generic function type (a form of unification), but we currently don't do that in conditional types. Instead, function type parameters are erased to their constraints and we infer from those. In the example above, that causes us to infer {} for the infer R type parameter.
The proper fix for this would be have type inference perform instantiation of a source type in the context of a target type when the source type is a generic function type.
It's a design limitation, but Suggestion and Need proposal were added instead of closing. Does this mean there's a slim chance this could land eventually? This specific use case of a generic input type mapped onto the output is really common and this issue is stopping quite a few use cases from being typed properly.
Any new thoughts/insights? IMHO, now that v3.0 is out, this issue has become more interesting. It looks like this issue is the cause of this behavior:
typeBindConsumer<T,Mextends<A>(a: A)=>any>=Mextends((a: T)=>inferU)
? U
: neverconsta: BindConsumer<number,<U>(a: U) =>{boxed: U}>={boxed: 2,// infers the type: { boxed: {}}}
If (strong if) this is a bug/potential feature, then fixing it will allow for all sorts of neat types like:
// Can be used for function declarations like zip and even conditional mapping.// mapped types on tuples could also make this work.typeMapTuple<Tupleextendsany[],Mappingextends<T>(a: T)=>any>=never// long and tedious// Good for all manner of composition/piping.// Folds the tuple's types into one by inferring the return of Join.// mapped types on tuples probably isn't good enough for this?typeFoldTuple<Tupleextendsany[],Joinextends<T,U>(a: T,b: U)=>any,Seed>=never// longer and more tedious
Though, any type made this way can be expressed, albeit more verbosely.
Anders Hejlsberg (@ahejlsberg) The fix for this was planned to be in TypeScript 2.9 milestone, but then it has been removed (judging from #22615 history). Is there any plan to include this in the milestones of 4.1, 4.2 or higher versions? Since it might have many use cases for higher order functions
Activity
ahejlsberg commentedon Mar 21, 2018
Repeating my comments from #22615 as they apply equally here:
It is effectively a design limitation. We have the concept of instantiating a generic function type in the context of a non-generic function type (a form of unification), but we currently don't do that in conditional types. Instead, function type parameters are erased to their constraints and we infer from those. In the example above, that causes us to infer
{}for theinfer Rtype parameter.The proper fix for this would be have type inference perform instantiation of a source type in the context of a target type when the source type is a generic function type.
SimonMeskens commentedon Apr 20, 2018
It's a design limitation, but Suggestion and Need proposal were added instead of closing. Does this mean there's a slim chance this could land eventually? This specific use case of a generic input type mapped onto the output is really common and this issue is stopping quite a few use cases from being typed properly.
DuncanWalter commentedon Aug 11, 2018
Any new thoughts/insights? IMHO, now that v3.0 is out, this issue has become more interesting. It looks like this issue is the cause of this behavior:
If (strong if) this is a bug/potential feature, then fixing it will allow for all sorts of neat types like:
Though, any type made this way can be expressed, albeit more verbosely.
fazouane-marouane commentedon Feb 4, 2019
Hi,
I'll add a simple example related to this:
thomasmikava commentedon Jul 18, 2020
Anders Hejlsberg (@ahejlsberg) The fix for this was planned to be in TypeScript 2.9 milestone, but then it has been removed (judging from #22615 history). Is there any plan to include this in the milestones of 4.1, 4.2 or higher versions? Since it might have many use cases for higher order functions
thomasmikava commentedon Jul 18, 2020
One of the use cases that can be achieved by fixing this limitation: