Skip to content

Export the options types for hooks such as useQuery, useLazyQuery, and useMutation. #5172

@minazukie

Description

@minazukie

Hello! I wrote a hook to unify calling all query and mutation APIs from the store in the current React context. My hook takes three parameters: the API endpoint path, the params, and the options. Since the options type is not exported directly, I have to use Parameters on TypedUseQuery to infer the Options type. It would be more convenient if the Options type could be exported directly.

type UseQuery<T extends EndpointDefinitions> = <
  K extends keyof T & string,
  Definition extends NonNullable<T[K]['Types']> = NonNullable<T[K]['Types']>,
>(
  path: K,
  arg?: Definition['QueryArg'],
  options?: Parameters<TypedUseQuery<Definition['ResultType'], Definition['QueryArg'], Definition['BaseQuery']>>[1],
) => TypedUseQueryHookResult<Definition['ResultType'], Definition['QueryArg'], Definition['BaseQuery']>;

const { data: data1 } = useQuery('api1.getUser', { id: 1 }, { refetchOnFocus: true });
const { data: data2 } = useQuery('api2.getStar', { id: 2 }, { refetchOnFocus: true });

If permitted, I could also submit a PR. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    RTK-QueryIssues related to Redux-Toolkit-QueryTypeScriptIssues related to TypeScript.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions