There are two different API namespaces known as user
and project
. I am attempting to develop a versatile function that can determine the method type of the respective API namespace object.
const user = {
async getUser(id: string): Promise<string> {
return 'user'
}
}
const project = {
async getProject(): Promise<string> {
return 'project'
}
}
const apis = {
user,
project
};
interface UseApiQueryResponse<Data> {
loading: boolean;
data: Data | undefined;
}
export function useApiQuery<Params, Data>(api: ??, params: Params): UseApiQueryResponse<Data> {
return api(params)
.then((res: string) => ({loading: false, data: res}));
}
Example usage on the customer side:
useApiQuery(apis.user.getUser, {id: '1'});
useApiQuery(apis.project.getProject);
I am looking for a way to properly type the api
argument, which could be any API method. It would be beneficial to automatically determine or restrict the return value type Data
based on the type of api