0

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>

1 Answer 1

1

I believe this is currently not possible directly, as there's no partial inference for generics, see https://github.com/microsoft/TypeScript/issues/10571 and https://github.com/microsoft/TypeScript/issues/20122 (the latter is almost exactly this same code). There may be workarounds, but they won't be nice.

However proper support for this is slowly coming! https://github.com/microsoft/TypeScript/pull/26349 is a currently open PR for TypeScript that would allow you to call your function like so:

const getAllSites = useAsyncCallback<{x: string}, _>('getAll');

Here the _ indicates a generic variable that you'd like to be inferred, and will require normal inference behaviour for that var (much like you see if you only have one generic arg, where this is inferred automatically).

This is not yet confirmed or merged behaviour for TypeScript, but if you'd like it to be you can +1 that issue & PR, and that'll help nudge them forwards to a proper solution.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.