Describe the bug#
When Defining getNextPageParam Before queryFn in useInfiniteQuery (same issue with infiniteQueryOptions)
lastPage is inffered as unknown
It works well
useInfiniteQuery({
queryKey: ["data"],
initialPageParam: 1,
queryFn: ({ pageParam }) => getData(pageParam),
getNextPageParam: (lastPage) =>
lastPage.hasNext ? lastPage.page + 1 : undefined,
});
It doesn’t work well.
lastPage in getNextPageParam is inffered as unknown
useInfiniteQuery({
queryKey: ["data"],
initialPageParam: 1,
getNextPageParam: (lastPage) =>
lastPage.hasNext ? lastPage.page + 1 : undefined,
queryFn: ({ pageParam }) => getData(pageParam),
});
Your minimal, reproducible example#
https://codesandbox.io/p/devbox/dkhrfg
Steps to reproduce#
- Go to codesandbox link
Expected behavior#
I expect that lastPage is inferred as the return type of getData.
useInfiniteQuery({
queryKey: ["data"],
initialPageParam: 1,
getNextPageParam: (lastPage) =>
lastPage.hasNext ? lastPage.page + 1 : undefined,
queryFn: ({ pageParam }) => getData(pageParam),
});
How often does this bug happen?#
None
Screenshots or Videos#
No response
Platform#
not affect
Tanstack Query adapter#
None
TanStack Query version#
latest
TypeScript version#
v5.6.3
Additional context#
No response
This is a known issue because of how TypeScript currently infers types. We have a lint rule that should catch this:
https://tanstack.com/query/v5/docs/eslint/infinite-query-property-order