I want to create a generic function that only allows calls to functions specified in an interface.
interface MyApi {
add(x: number, y:number) : number;
say(s:string): string;
A: number;
}
type OnlyFuncs<T> = Pick<T, { [K in keyof T]: T[K] extends (...args:any) => any ? K : never }[keyof T]>;
class ApiClient<T, Tfunc = OnlyFuncs<T>> {
public call<K extends keyof Tfunc, T2 = Tfunc[K]>(method:K, p:Parameters<T2>) {
}
}
I'd like to able to use my ApiClient class to call the two methods 'add' and 'say' from the function 'call'. I've managed to "filter out" member A from the MyApi interface, but I can't get the correct type T2 to get the parameters right for the 'p' argument.
Why is T2 not seen by the compiler as a function?