Skip to content

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

Open
@nizioleque

Description

@nizioleque

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-react-queryRelevant to openapi-react-query

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions