Skip to content
Source
Sign inSign up
TanStack/query

Type checking `select` works differently on `useQueries` compared to `useQuery` #6556

Send to my agent
Closedjtannas(GitHub profile, opens in a new tab) opened this issue 14 comments

Describe 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

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1WO1AGhQ21wE84BfOAMyghDgCIABGAQwDtVOBjANYB6KOg58YAWgCOOKOWYAoRXwg94IcgBEOnOAF44AbQCMxAEzEAzMQAsxAKzEAbAF1lKtbziay8gyRYcuQAFAiKcHCyFADS6OQAXMYA5OgAbuhcya6EEVHBAGJcSSEAlAYAfHAACgwgwBgAdKKoEAA2GSGaOpyluZEYbegSJRxQUOX6VWNQjbTAbTC4IcCVcKsApHDmBvqGAAx9ilSlyqrqPuR++KgBaEG4N2HReARJRnnhkZEv5HGJKXSmWy-W+vyKJUmVVqjAa6GaBHanW6ug4R2+cEGwxgo3GULgMzmCyWUBWa02212B3R1FyrhOiiAA

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

  1. jtannas(GitHub profile, opens in a new tab) changed the title

  2. I think it's a known limitation.

    Possible workarounds:

  3. was gonna propose the first workarounds but the second one seems fine too

  4. TkDodo added labelstypes

  5. TkDodo added labelsknown limitation

  6. TkDodo added labelshelp wanted

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

  8. Thanks for the tips :) I've been annotating the inputs to work around it but was shying away from queryOptions because these are suspense queries. I tried some workarounds with satisfies UseSuspenseQueryOptions to narrow it down, but no luck there.

    Any suggestions there? Maybe a suspenseQueryOptions helper?

  9. You can pass queryOptions just fine to useSuspenseQueries

    playground

  10. yep - was just hoping for something that'd prevent people slipping in placeholderData/enabled since that caught us pretty badly when we started transitioning to suspense

  11. birkskyum(GitHub profile, opens in a new tab) added labelspackage: react-query

  12. @TkDodo That's an interesting issue. Can I take a look at this?

  13. yes please 🙏

  14. TkDodo assignedminseong0324

  15. I think it's more complicated than I expected. May I take a moment to look at it? ;)

  16. One interesting thing I discovered while combining useSuspenseQueries with queryOptions and select is that you can use a satisfies clause 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 original queryFn's return type, but you can fix that by adding an explicit return type to select.

  17. TkDodo added labelshas workaround

  18. TkDodo unassignedminseong0324

  19. 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!

  20. Hi! I’d like to work on this issue.

    I’ll investigate the difference in type checking for the select option between useQueries and useQuery, 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

  21. Hi! I'd like to take this issue.

    I'll look into the type mismatch for the select option on useQueries and work on a fix with a small reproduction and a PR.

  22. 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).

  23. TkDodo closed this

Sign in to join this conversation.