Skip to content
Source
Sign inSign up
TanStack/query

`useQueries` + `skipToken` looses `.data` type inference #7035

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

Describe the bug

When using useQueries with skipToken, queryResult.data is inferred as unknown instead of the actual return type of the queryFn.

const [withEnabled] = useQueries({
  queries: [{ queryKey: ["withEnabled"], queryFn: () => 1, enabled: true }],
});

const [withSkipToken] = useQueries({
  queries: [
    { queryKey: ["withSkipToken"], queryFn: true ? () => 1 : skipToken },
  ],
});

withEnabled.data;
//           ^? number | undefined
withSkipToken.data;
//             ^? unknown

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/tanstack-react-query-usequeries-skiptoken-issue-wdk8m5

Steps to reproduce

  • Open repro
  • Hover over withEnabled.data and withSkipToken.data to see the types

Expected behavior

I expected withSkipToken.data to be inferred as number | undefined instead of unknown.

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

@tanstack/react-query v5.25.0

Tanstack Query adapter

react-query

TanStack Query version

v5.25.0

TypeScript version

v5.3.3

Additional context

No response

  1. @Jaaneek can you take a look here? I'm afraid I'm no expert with the userQueries typings 😅

  2. @vjee quick fix is to add "as const" after queries array.

     queries: [
        { queryKey: ["withSkipToken"], queryFn: true ? () => 1 : skipToken },
      ] as const

    For some reason typescript doesnt make the array "readonly" by default when passing skipToken @TkDodo

    I'm looking for a solution that wouldn't require the "as const"

  3. Thanks for the quick fix. Great feature btw! Well done 👏

  4. I found a fix, pull request incoming

  5. TkDodo added labelsbug

  6. TkDodo added labelstypes

  7. Seems like some other tests are failing once thix is fixed.

    Can't find a good solution at this moment.

    Feel free to contribute anyone!

    temporary fix is add as const

     queries: [
        { queryKey: ["withSkipToken"], queryFn: true ? () => 1 : skipToken },
      ] as const
  8. TkDodo added labelshelp wanted

  9. TkDodo added labelshas workaround

  10. I've worked on the issue in question. Please take a look. @TkDodo

  11. TkDodo closed this

Sign in to join this conversation.