Skip to content
Source
Sign inSign up
TanStack/query

[react-query] type error for infinity query defaults in QueryClient #8218

Send to my agent
Closedaizerin(GitHub profile, opens in a new tab) opened this issue 6 comments

Describe the bug

I recently migrated from react-query v4 and this code now reports typescript error.

It does not accept values for infinite query.

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      getNextPageParam: (lastPage: any) => lastPage?.nextCursor ?? undefined,
    },
  },
});
Type '{ getNextPageParam: (lastPage: any) => any; }' is not assignable to type 'OmitKeyof<QueryObserverOptions<unknown, Error, unknown, unknown, QueryKey, never>, "suspense" | "queryKey", "strictly">'.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/jtqm8f

Steps to reproduce

Just write this code

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      getNextPageParam: (lastPage: any) => lastPage?.nextCursor ?? undefined,
    },
  },
});

Expected behavior

Typescript should be happy

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Any

Tanstack Query adapter

react-query

TanStack Query version

5.59.16

TypeScript version

5.6.3

Additional context

No response

  1. I know, and I think this is wrong. Defaults for infinity query should be included in types (as they were in react-query v4).

    It also works at runtime. getNextPageParam is used as the default for useInfinityQuery.

  2. Ok, then it is kinda inconsistency because the implementation differs from its typings.

  3. TkDodo added labelstypes

  4. TkDodo added labelspackage: query-core

  5. we moved infinite query types into their own interface because getNextPageParam is now required for those:

    https://github.com/TanStack/query/blob/18953be6f61553f4ed3dd888945dfa1683aa6e40/packages/query-core/src/types.ts#L410-L425

    It therefore doesn’t really make sense to set it globally (you’ll get an error on useInfiniteQuery if you don’t provide it) - same as for initialPageParam.

    We could add it as Partial to the defaultOptions but how do you intend to use it on useInfiniteQuery then ?

  6. I have several infinity queries with the same getNextPageParam function. So I don't want to repeat it each time and just set it as default.

  7. So I don't want to repeat it each time and just set it as default.

    I mean, even if we add these types for the default on the QueryClient, you would still get a type error on useInfiniteQuery. I don’t want to make them optional there as well because they are important for type-inference.

    Your best option is to just create a sharable object that contains getNextPageParam and spread that into your useInfiniteQuery calls:

    const myDefaults = {
      getNextPageParam: () => ...,
      initialPageParam: 0,
    } as const
    
    useInfiniteQuery({
      queryKey,
      queryFn,
      ...myDefaults
    }
  8. TkDodo closed this

  9. TkDodo added labelswontfix

  10. TkDodo added labelshas workaround

Sign in to join this conversation.