Initially, I have a class containing attributes and methods. My goal is to filter and retrieve only the keys of the methods.
I created a utility type for this purpose and it worked smoothly:
type FunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends (...args: any) => any ? K : never;
}[keyof T];
export type OnlyFunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>;
Now, I aim to extract only the method keys that return a result indexable by a string, specifically those that possess a 'body' property. This is necessary as I intend to construct the following function with this specific return type.
public async call<K extends keyof OnlyFunctionProperties<T>>(
method: K,
...params: Parameters<T[K]>
): Promise<ReturnType<T[K]>['body']> {
const response = {};
return response as any;
}
However, I am currently encountering the following error:
Type '"body"' cannot be used to index type 'ReturnType<T[K]>'
I attempted to modify the FunctionPropertyNames utility but so far, no viable solutions have emerged. Here is a TypeScript Playground link demonstrating the issue.