I have this playground and this code
export interface RemoteMethods<R> {
getAll: (url: string) => Promise<R>;
create: (url: string, model: R) => Promise<void>;
}
export type AsyncFunctionReturnType<
R,
Method extends keyof RemoteMethods<R>
> =
{
[key in Method]: RemoteMethods<R>[Method];
};
export const makeRemoteMethods = <R>(
) => {
return {
getAll: async (url: string) => {
return Promise.resolve(undefined);
},
create: async (url: string, model: R) => {
return Promise.resolve(undefined);
},
};
};
export const useAsyncCallback = <
R,
K extends keyof ReturnType<typeof makeRemoteMethods>
>(
method: K,
): AsyncFunctionReturnType<R, K> => {
const remoteMethods = makeRemoteMethods<R>();
const m = { [method]: remoteMethods[method] } as unknown as {
[key in K]: RemoteMethods<R>[K];
};
return {
...m,
};
};
const getAllSites = useAsyncCallback<{x: string}, 'getAll'>('getAll');
I want to somehow infer the second type argument in this function call
const getAllSites = useAsyncCallback<{x: string}, 'getAll'>('getAll');
I want to call the function like:
const getAllSites = useAsyncCallback<{x: string}>('getAll');
and somehow infer the type argument K extends keyof ReturnType<typeof makeRemoteMethods>