Skip to content
Source
Sign inSign up
TanStack/query

infiniteQueryOptions can't infer type if queryFn uses pageParam #7485

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

Describe the bug

The issue is that typescript can't infer type if queryFn passed to options uses pageParam.

Your minimal, reproducible example

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgVwM4FMCSA7AZsbYGdARWXSgE8AaOA-Q4siygeTBmAm1TgF84uKBBBwA5AAEYAQx4yAxgGsA9FHTT5MALQBHclTEBYAFAn53VPAgcuPOAF46eAkVL62NiwAoEJ5crhAoIA9AH4TILg9FgBpdEoALjgAbTFiSzEAXWoIoJdOaQAbAAVpAHN0UqhpECSABhzjSIqYADl0AA8YUoqqmqSvABNpGVoikvL0VFpC6Use9ABKBwA+RFzIwP9NnZDwpt21GGQobDhZ+cm4AGo4AEYN-kbI6KoAMWwBpDBJvtE+Zb2NbFYQgYAYAB0alQEEKADd0D5HpFtjswsigjCQOhWsgQAAjChJH69aTVUS3ACsjwBjQBJjMFisnh4ACYHE4GK5mFR2JxvL5jKjIuiDoFXpQ4okUmkpjAss88oxgEUFn96orAi12l01WT+nAhiNpGNChMKtNznNupNAWtBbs4MLHXsMYEjiczhcbRUbvcaZqou4PgM7XAQSJwegoVNYQikWLNlicXjCVAktTE7STACgA

Steps to reproduce

const options = infiniteQueryOptions({
    queryKey: ['test'],
    initialPageParam: 0,
    getNextPageParam: (data, allPages, lastPage) => {
        //                               ^ in this case data is unknown
        return lastPage + 1
    },
    queryFn: ({ pageParam }) => Promise.resolve({
        someNumber: pageParam + 5
    }),
})

but if pass

queryFn: () => Promise.resolve({
    someNumber: 5
}),

in this case TS inference works as expected

Expected behavior

Types works coddect

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

OS: macOs

Tanstack Query adapter

react-query

TanStack Query version

5.40.0

TypeScript version

5.4.5

Additional context

No response

  1. to get type inference for getNextPageParam, it must come after queryFn. It's a known typescript limitation.

    fixed playground

    duplicate of:

  2. TkDodo closed this

  3. TkDodo added labelsduplicate

Sign in to join this conversation.