Type checking `select` works differently on `useQueries` compared to `useQuery` #6556
Send to my agentDescribe the bug#
When using useQuery, TS correctly infers the argument type of the select query option to be the return type of the queryFn data.
Applying the same settings to useQueries it loses that link and changes it to any.
The linked TS sandbox shows the issue better than I can explain it though.
Your minimal, reproducible example#
Steps to reproduce#
See linked Typescript playground to a minimal reproducible example
Expected behavior#
I would like the type checking of select on useQueries to work as well as it does on useQuery.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- Ubuntu 22.04
Tanstack Query adapter#
react-query
TanStack Query version#
v5.3.2
TypeScript version#
v5.14.0
Additional context#
No response
jtannas(GitHub profile, opens in a new tab) changed the title
was gonna propose the first workarounds but the second one seems fine too
TkDodo added labelstypes
TkDodo added labelsknown limitation
TkDodo added labelshelp wanted
I'll leave this open if someone wants to take a look, but given that we have good workarounds, it's not a priority for me.
- annotate the input to
select: playground - use the
queryOptionshelper: playground
Thanks for the tips :) I've been annotating the inputs to work around it but was shying away from
queryOptionsbecause these are suspense queries. I tried some workarounds withsatisfies UseSuspenseQueryOptionsto narrow it down, but no luck there.Any suggestions there? Maybe a
suspenseQueryOptionshelper?- annotate the input to
You can pass
queryOptionsjust fine touseSuspenseQueriesyep - was just hoping for something that'd prevent people slipping in
placeholderData/enabledsince that caught us pretty badly when we started transitioning to suspensebirkskyum(GitHub profile, opens in a new tab) added labelspackage: react-query
@TkDodo That's an interesting issue. Can I take a look at this?
yes please 🙏
TkDodo assignedminseong0324
I think it's more complicated than I expected. May I take a moment to look at it? ;)
One interesting thing I discovered while combining
useSuspenseQuerieswithqueryOptionsandselectis that you can use asatisfiesclause to make it work:useSuspenseQueries({ queries: [ { ...someQueryOptions(), select: (data) => ({ // <--- `data` is `any` ...data, oldField: newValue, }), }, ], }); useSuspenseQueries({ queries: [ { ...someQueryOptions(), select: (data) => ({ // <--- `data` is inferred from `queryFn` ...data, oldField: newValue, }), } satisfies ReturnType<typeof someQueryOptions>, ], });Note:
select's return type might differ from the originalqueryFn's return type, but you can fix that by adding an explicit return type toselect.TkDodo added labelshas workaround
TkDodo unassignedminseong0324
I looked into this and tried adding function overloads to provide contextual typing for
select.It works for inline queries, but breaks explicit type parameters like
useQueries<[[string, Error, Data]]>({...}). TypeScript can't distinguish between inferred types and explicit type parameters in overload resolution, so I couldn't find a way to support both.🥲Let me know if you have any ideas for a different approach!
Hi! I’d like to work on this issue.
I’ll investigate the difference in type checking for the
selectoption betweenuseQueriesanduseQuery, and try to align the behavior by updating the relevant TypeScript definitions. I’ll also create a minimal reproduction to verify the fix before opening a PR.Please let me know i
Hi! I'd like to take this issue.
I'll look into the type mismatch for the
selectoption onuseQueriesand work on a fix with a small reproduction and a PR.no good solution has come up in over a year and we have a good workaround with
queryOptions, which is the preferred abstraction anyways (we even have a lint rule for this).TkDodo closed this
Sign in to join this conversation.
I think it's a known limitation.
Possible workarounds:
select: playgroundqueryOptionshelper: playground