Skip to content
Source
Sign inSign up
TanStack/query

useInfiniteQuery `data.pageParams` is always unknown type #6628

Closednickqweaver(GitHub profile, opens in a new tab) opened this issue 8 comments

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

  1. Make usage of useInfiniteQuery and pass all required options
  2. 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

  1. this was a limitation we accepted when we implemented the type inference for pageParam in the queryFn and in getNextPageParam because we didn't want to add the complexity of more type parameters everywhere.

    This is only relevant if you need the pageParams during rendering (returned from useInfiniteQuery), and I don't think that's very common. What do you need them for?

  2. this was a limitation we accepted when we implemented the type inference for pageParam in the queryFn and in getNextPageParam because we didn't want to add the complexity of more type parameters everywhere.

    This is only relevant if you need the pageParams during rendering (returned from useInfiniteQuery), 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 cursor of 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 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 can technically achieve the same thing by grabbing the last page, then looking at pageInfo.startCursor within the response I just figured this would be easier, and it still technically works I just have to assert the type.

  3. 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 pageParam to the data returned from the queryFn, then you could grab it directly from data and it would be typed ?

  4. I think you could work around that by just adding the pageParam to the data returned from the queryFn, then you could grab it directly from data and it would be typed ?

    Yeah that's not a bad idea. Thanks @TkDodo

  5. I can leave this issue open if you want to work on it, but otherwise, I'd close it

  6. TkDodo added labelstypes

  7. TkDodo added labelshas workaround

  8. TkDodo added labelsknown limitation

  9. All good, I was mostly curious if this was a known issue or not. I will close it. Thanks again

  10. I think this issue could be reopened. I ran into this exact scenario and have had to opt for the previously mentioned workaround.

  11. @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 pageParams during 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.