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
Using ReturnType with generic function parameters and generic property access #26856
Workaround (duplicate similar ReturnType-like type locally):
exporttypeInvokeResult<Textends(...args: A)=>any,Aextendsany[]>=Textends(...args: A)=> infer R ? R : neverexportfunctioninvoker<Kextendsstring|number|symbol,Aextendsany[]>(key: K, ...args: A){return<TextendsRecord<K,(...args: A)=>any>>(obj: T): InvokeResult<T[K],A>=>obj[key](...args)}
Expected behavior: Valid program and use of ReturnType.
Actual behavior:
Type '(...args: A) => any' does not satisfy the constraint '(...args: any[]) => any'.
Types of parameters 'args' and 'args' are incompatible.
Type 'any[]' is not assignable to type 'A'.
Related Issues: No, this is out of my skillset to debug tonight. Logging for posterity and help 😄 I did try different workarounds and found I couldn't workaround it with any usage of property access so I added that to the title. For instance, the exact same code inlined results in an empty interface:
exportfunctioninvoker<Kextendsstring|number|symbol,Aextendsany[]>(key: K, ...args: A){return<TextendsRecord<K,(...args: A)=>any>>(obj: T): T[K]extends(...args: A)=> infer R ? R : never=>obj[key](...args)}constresult=invoker('test',true)({test: (abc: boolean)=>123})
More minimal repro which makes me realise it may not be an issue but maybe an enhance to ReturnType to specify the generic type parameters:
In --strictFunctionTypes mode this produces an error similar to the one you're seeing, the issue being that (...args: A) => string does not satisfy the constraint (...args: any[]) => any because any[] is not assignable to A. It seems that we need to be a little more permissive in the way we check assignability of rest parameters with type any[].
Reacted by Blake Embrey, Nebez Briefkani, Ben and Simon Weaver
Less safety version of ReturnType but working for all inputs:
typeUnsafeReturnType<T>=Textends(...args: any[])=> infer R ? R : any;
Update1
But inference not working properly for all cases.
Cases above working correct.
But not working for union type rest parameters case (with enabled strictFunctionTypes):
constfoo=(...args: [string,number]|[1])=>({type: 'type',
args,});typet=UnsafeReturnType<typeoffoo>;// any
Reacted by Sean Matheson, Mitchell Heddles, Etienne Martin, Mo Doaie and Tom Ardern
Reacted by Sean Matheson, Mitchell Heddles, Etienne Martin and Jorge Castro
Activity
ahejlsberg commentedon Sep 3, 2018
Yeah, that error is unfortunate. Here's a minimal repro:
In
--strictFunctionTypesmode this produces an error similar to the one you're seeing, the issue being that(...args: A) => stringdoes not satisfy the constraint(...args: any[]) => anybecauseany[]is not assignable toA. It seems that we need to be a little more permissive in the way we check assignability of rest parameters with typeany[].Voronar commentedon Jan 18, 2019
Less safety version of
ReturnTypebut working for all inputs:Update1
But inference not working properly for all cases.
Cases above working correct.
But not working for union type rest parameters case (with enabled
strictFunctionTypes):