Order matters to useInfiniteQuery options and TS #7268
Send to my agentDescribe the bug#
I am implementing the sort-keys eslint rule in my codebase and it seems that the order of the options given to useInfiniteQuery matter. If I don't have getNextPageParam and getPreviousPageParam after queryFn I get a TS error on queryFn stating
No overload matches this call.
Fine:
useInfiniteQuery({
enabled: true,
initialPageParam: "",
queryFn: async () => {
const data = await getThings();
return { links, things: data };
},
queryKey: "key",
getNextPageParam: ({ links }) => links?.next,
getPreviousPageParam: ({ links }) => links?.previous,
})
Errors
useInfiniteQuery({
enabled: true,
getNextPageParam: ({ links }) => links?.next,
getPreviousPageParam: ({ links }) => links?.previous,
initialPageParam: "",
queryFn: async () => {
const data = await getThings();
return { links, things: data };
},,
queryKey: "key",
})
I tried giving types to useInfiniteQuery but this didn't help.
Your minimal, reproducible example#
https://snack.expo.dev/5TZgw1IVp4_3SJXK7PF5R
Steps to reproduce#
I've attached the code as an expo snack, but without TS this won't cause any issues and is functional.
- Clone the snack or start a new TS project and copy in the code.
- Ensure the ordering of the
useInfiniteQueryoptions is alphabetical. - Observe the TS error.
- Move the
getNext/PreviousPageParamkeys to the bottom of the options object. - Observe the TS error goes away.
Expected behavior#
Order of options in an object should not matter for automatic typing and TS compatibility.
How often does this bug happen?#
Every time
Screenshots or Videos#
Platform#
- OS: macOS
- Browser: React Native
Tanstack Query adapter#
react-query
TanStack Query version#
v5.24.1
TypeScript version#
v5.3.3
Additional context#
I tried updating to latest react-query & typescript but didn't see a difference, though it may be more complex of a dependency chain issue if that's it.
TkDodo closed this
TkDodo added labelsduplicate
@TkDodo there is no explanation in that closed duplicate as to why this is happening and no plans to fix.
Can you give me at least some explanation? The other thread looks like you're talking to people with a ton of context and just say 🤷🏻♂️
it's a TypeScript limitation. Have a look at the minimal reproduction with consume / produce posted by Mateusz here:
produce === queryFn - it produces the return type for
TDataconsume === getNextPage - it consumes the type fromqueryFnas input datain some situations, consume can't come before produce in an object. Again, the only way to fix this is to wait until typescript has fixed this limitation, in which case it will just start to work. There is nothing we can do here.
@TkDodo thanks, I appreciate the response 🙂
Sign in to join this conversation.
duplicate: