openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

`typeof api.queryOptions` has `path: never` in parameter list and return type `any`

Open nizioleque opened this issue 9 months ago • 0 comments

openapi-react-query version

0.2.10

Description

typeof api.queryOptions (where api is const api = createTanstackQueryClient(client)) does not have the expected type.

Parameters<typeof api.queryOptions> is [method: HttpMethod, path: never, undefined?, (Omit<UseQueryOptions<never, never, any, QueryKey<paths, HttpMethod, never>>, "queryKey" | "queryFn"> | undefined)?]

As a result,

const params: Parameters<typeof api.queryOptions> = ["get", "/flows"];

causes an error on "/flows":

Type string is not assignable to type never.

Also, ReturnType<typeof api.queryOptions> is any, but this is more expected as the return type depends on the parameter values, right? However, it would still be useful to have ReturnType provide the actual return type of the function.

Reproduction

I am trying to create a reusable hook for invalidating queries like this:

function useInvalidateQuery(...args: Parameters<typeof api.queryOptions>) {
  const queryClient = useQueryClient();
  const { queryKey } = api.queryOptions(...args);
  return () => queryClient.invalidateQueries({ queryKey });
}

When trying to use the hook, there is always a type error on the second parameter because its type is never.

I also attempted using the library types more directly:

...args: Parameters<OpenapiQueryClient<paths>["queryOptions"]>

but it has the same issue.

As a workaround I attempted this:

function useInvalidateQuery(
  queryOptions: ReturnType<typeof api.queryOptions>,
) {
  const queryClient = useQueryClient();
  const { queryKey } = queryOptions;
  return () => queryClient.invalidateQueries({ queryKey });
}

but then the type of queryOptions is any.

Expected result

I expect Parameters<typeof api.queryOptions> to behave the same as the parameters of api.queryOptions when calling it directly. Similarly, I expect ReturnType to provide an actual return type.

Extra

nizioleque avatar Feb 27 '25 13:02 nizioleque