Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useQueries + skipToken looses .data type inference #7035

Closed
vjee opened this issue Mar 6, 2024 · 7 comments · Fixed by #7150
Closed

useQueries + skipToken looses .data type inference #7035

vjee opened this issue Mar 6, 2024 · 7 comments · Fixed by #7150
Labels
bug Something isn't working has workaround help wanted Extra attention is needed types

Comments

@vjee
Copy link

vjee commented Mar 6, 2024

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

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 6, 2024

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

@Jaaneek
Copy link
Contributor

Jaaneek commented Mar 6, 2024

Looking into it!

@Jaaneek
Copy link
Contributor

Jaaneek commented Mar 6, 2024

@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"

@vjee
Copy link
Author

vjee commented Mar 6, 2024

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

@Jaaneek
Copy link
Contributor

Jaaneek commented Mar 6, 2024

I found a fix, pull request incoming

@Jaaneek
Copy link
Contributor

Jaaneek commented Mar 9, 2024

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

@gwansikk
Copy link
Contributor

gwansikk commented Mar 21, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working has workaround help wanted Extra attention is needed types
Projects
None yet
4 participants