useInfiniteQuery `data.pageParams` is always unknown type #6628
Describe the bug#
When using useInfiniteQuery, data.pageParams is always unknown[] | undefined. It does seem to be inferring correctly in the infinite query options. In my example I type casted everything to see if I could get it to return correctly but no luck.
Your minimal, reproducible example#
https://codesandbox.io/p/sandbox/exciting-montalcini-vq23jt?file=%2Fsrc%2FApp.tsx%3A10%2C39
Steps to reproduce#
- Make usage of
useInfiniteQueryand pass all required options - Use the query, access
data.pageParams
You will see pageParams is always unknown[] | undefined
Expected behavior#
I would expect data.pageParams to infer the same type as the TPageParam generic passed to GetNextPageParamFunction
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- OS: macOS
- Browser: Chrome
- Browser Version: 120.0.6099.129
Tanstack Query adapter#
react-query
TanStack Query version#
5.17.0
TypeScript version#
5.3.3
Additional context#
No response
this was a limitation we accepted when we implemented the type inference for
pageParamin thequeryFnand ingetNextPageParambecause we didn't want to add the complexity of more type parameters everywhere.This is only relevant if you need the
pageParamsduring rendering (returned fromuseInfiniteQuery), and I don't think that's very common. What do you need them for?I'm using Relay style cursor based pagination, so each page param is the
cursorof the last item. My UI has a button at end of the page results to "Load More", when user clicks Load More I auto scroll to the first item in the new page.I'm using
pageParamsby listening for changes todata, grabbing the lastpageParam(which is an id/data attribute on an element) and then scrolling that element into view. I can technically achieve the same thing by grabbing the last page, then looking atpageInfo.startCursorwithin the response I just figured this would be easier, and it still technically works I just have to assert the type.I'm using pageParams by listening for changes to data, grabbing the last pageParam (which is an id/data attribute on an element) and then scrolling that element into view.
I think you could work around that by just adding the
pageParamto thedatareturned from thequeryFn, then you could grab it directly fromdataand it would be typed ?I think you could work around that by just adding the
pageParamto thedatareturned from thequeryFn, then you could grab it directly fromdataand it would be typed ?Yeah that's not a bad idea. Thanks @TkDodo
I can leave this issue open if you want to work on it, but otherwise, I'd close it
TkDodo added labelstypes
TkDodo added labelshas workaround
TkDodo added labelsknown limitation
All good, I was mostly curious if this was a known issue or not. I will close it. Thanks again
nickqweaver(GitHub profile, opens in a new tab) closed this
I think this issue could be reopened. I ran into this exact scenario and have had to opt for the previously mentioned workaround.
@dburles do you want to work on it? As I’ve explained, it’s a known limitation and it also doesn’t come up too often, because most people don’t use the
pageParamsduring rendering. That’s why I won’t spend any time on it currently, but please feel free to give it a shot if you want.
Sign in to join this conversation.
this was a limitation we accepted when we implemented the type inference for
pageParamin thequeryFnand ingetNextPageParambecause we didn't want to add the complexity of more type parameters everywhere.This is only relevant if you need the
pageParamsduring rendering (returned fromuseInfiniteQuery), and I don't think that's very common. What do you need them for?