-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
Description
TypeScript Version: 3.1.0-dev.20180901
Search Terms: ReturnType, generic, invoke, invoker, property
Code
function invoker <K extends string | number | symbol, A extends any[]> (key: K, ...args: A) {
return <T extends Record<K, (...args: A) => any>> (obj: T): ReturnType<T[K]> => obj[key](...args)
}
const result = invoker('test', true)({ test: (a: boolean) => 123 })Workaround (duplicate similar ReturnType-like type locally):
export type InvokeResult <T extends (...args: A) => any, A extends any[]> = T extends (...args: A) => infer R ? R : never
export function invoker <K extends string | number | symbol, A extends any[]> (key: K, ...args: A) {
return <T extends Record<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:
export function invoker <K extends string | number | symbol, A extends any[]> (key: K, ...args: A) {
return <T extends Record<K, (...args: A) => any>> (obj: T): T[K] extends (...args: A) => infer R ? R : never => obj[key](...args)
}
const result = 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:
type Test <T extends (...args: A) => any, A extends any[]> = ReturnType<T>Erikvv, nebez, ben-chin, jcastrov, Arrow7000 and 2 more
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue