The generic return type R should be inferred as string, but it’s instead unknown. Why and how to make it to be inferred correctly?
function call<Args extends any[], R, F extends ((...args: Args) => R)>(
fn: F, ...args: Args
): R {
return fn(...args)
}
function some<T>(a: T[]): string { return "" }
let v = call(some, [1]) // => inferred type of v is unknown
>Solution :
Use ReturnType<F> as the return type of your function to infer the type from the passed function instead of using the R generic
function call<Args extends any[], F extends ((...args: Args) => any)>(
fn: F, ...args: Args
): ReturnType<F> {
return fn(...args)
}