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 return type of a function with provided argument types #26043
I'm trying to create something similar to native ReturnType type. In comparison to the later, it should also consider generic types of arguments.
I guess the idea will be clear from the code below.
If it can be done now - I'd be happy to receive some links. Otherways it'd be great to know if there are any plans to cover that behavior. In case it could be achieved by another not-implemented feature I'll be glad for the link to an issue to be able to track its progress.
TypeScript Version: 2.9.2
Search Terms: infer function return type
Code
// it works with actual code:functionidentity<T>(a: T){returna;}constone=identity(1);// typeof one === 1 # everything is greatfunctioncall<A,R>(arg: A,fn: Func<A,R>): R{returnfn(arg);}consttwo=call(2as2,i=>i)// typeof two === 2 # everything is great// but won't work with typestypeFunc<A,R>=(arg: A)=>R;// ReturnTypeByArg is inspired by `ReturnType` from TS 2.8.*typeReturnTypeByArg<T,FextendsFunc<T,any>>=FextendsFunc<T, infer R> ? R : any;typeIdentity=<T>(a: T)=>T;typeResult=ReturnTypeByArg<4,Identity>;// Result === {} # not what I expect
Expected behavior:
I expect TS to be able to infer such return type
Mohamed Hegazy (@mhegazy) I guess #6606 is a much more complex feature request. Mine looks like only one case of "Call" type which was referenced by Trey Brisbane (@treybrisbane). And if such Call type is implemented it will cover my issue also.
But I thought my problem can be solved through new 2.8.* infer key-word. So I see it rather as a bug than a feature request.
It would be great if you provided me with a link to an issue about such Call type if one exists.
By the way, since #6606 is closed I'm not sure what do you mean by marking mine as a duplicate. Does it mean this issue won't be fixed?
The underlying scenario is the same. #6606 proposed using typeof as syntax, but you can see the same happening using a call syntax foo(number, string) without the typeof keyword.
The complexity in implementing either is applying overload resolution as a higher-order type operator. the overload resolution is extremely complicated process, that involved multiple passes and required multiple processes like contextual types, inference, and instantiations to interact. Enabling this, at least in the current compiler implementation, for higher order types is tantamount to rewriting the core of the compiler; something we are not looking forward to.
Activity
mhegazy commentedon Jul 30, 2018
Looks like a duplicate of #6606
int0h commentedon Jul 31, 2018
Mohamed Hegazy (@mhegazy) I guess #6606 is a much more complex feature request. Mine looks like only one case of "Call" type which was referenced by Trey Brisbane (@treybrisbane). And if such Call type is implemented it will cover my issue also.
But I thought my problem can be solved through new 2.8.*
inferkey-word. So I see it rather as a bug than a feature request.It would be great if you provided me with a link to an issue about such Call type if one exists.
By the way, since #6606 is closed I'm not sure what do you mean by marking mine as a duplicate. Does it mean this issue won't be fixed?
mhegazy commentedon Jul 31, 2018
The underlying scenario is the same. #6606 proposed using
typeofas syntax, but you can see the same happening using a call syntaxfoo(number, string)without thetypeofkeyword.The complexity in implementing either is applying overload resolution as a higher-order type operator. the overload resolution is extremely complicated process, that involved multiple passes and required multiple processes like contextual types, inference, and instantiations to interact. Enabling this, at least in the current compiler implementation, for higher order types is tantamount to rewriting the core of the compiler; something we are not looking forward to.
You can seem my reply in #6606 (comment) as well.
malash commentedon Dec 14, 2018
I believe that's not a bug.
infer Ris the union of all available types.As you can see the second argument of Func could be
any, as known as{}.So
infer Rshould be union of them and it's{}.int0h
typescript-bot commentedon Dec 16, 2018
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.