[react-query] type error for infinity query defaults in QueryClient #8218
Send to my agentDescribe 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
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.
getNextPageParamis used as the default for useInfinityQuery.Ok, then it is kinda inconsistency because the implementation differs from its typings.
TkDodo added labelstypes
TkDodo added labelspackage: query-core
we moved infinite query types into their own interface because
getNextPageParamis now required for those:It therefore doesn’t really make sense to set it globally (you’ll get an error on
useInfiniteQueryif you don’t provide it) - same as forinitialPageParam.We could add it as
Partialto thedefaultOptionsbut how do you intend to use it onuseInfiniteQuerythen ?I have several infinity queries with the same
getNextPageParamfunction. So I don't want to repeat it each time and just set it as default.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
getNextPageParamand spread that into youruseInfiniteQuerycalls:const myDefaults = { getNextPageParam: () => ..., initialPageParam: 0, } as const useInfiniteQuery({ queryKey, queryFn, ...myDefaults }TkDodo closed this
TkDodo added labelswontfix
TkDodo added labelshas workaround
Sign in to join this conversation.
Hi @aizerin as far as I can see,
getNextPageParamis not an option forQueryClient-> https://tanstack.com/query/latest/docs/reference/QueryClient.You might wanna use one of these: