Skip to content
Source
Sign inSign up
TanStack/query

[react-query] Type Inference Bug When Defining getNextPageParam Before queryFn in useInfiniteQuery #8301

Send to my agent
Closedjijiseong(GitHub profile, opens in a new tab) opened this issue 1 comment

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

  1. 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

  1. 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

  2. TkDodo closed this

  3. TkDodo added labelstypes

  4. TkDodo added labelshas workaround

  5. TkDodo added labelsknown limitation

Sign in to join this conversation.