Skip to content
Source
Sign inSign up
TanStack/query

useQueries not inferring type correctly, when using onSuccess and select #3994

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

Describe the bug

When using useQueries() with typed individual queries, the data type is not inferred when using onSuccess or select methods, and we get an error Parameter 'data' implicitly has an 'any' type.

With useQuery() it works fine and we can see the data's type: (parameter) data: number

Your minimal, reproducible example

https://codesandbox.io/s/wizardly-ride-w58pio?file=/src/App.tsx

Steps to reproduce

  1. Go to sandbox - https://codesandbox.io/s/wizardly-ride-w58pio?file=/src/App.tsx
  2. Check onSuccess in useQuery() options, it works fine.
  3. Check onSuccess and select in useQueries() options, it has an error.

Expected behavior

It is expected to have the inferred type also in useQueries()

How often does this bug happen?

No response

Screenshots or Videos

image

Platform

  • OS: mac OS
  • Browser: Opera

react-query version

v4.0.10

TypeScript version

v4.7.4

Additional context

No response

  1. TkDodo added labelsbug

  2. TkDodo added labelstypes

  3. TkDodo added labelspackage: react-query

  4. @artysidorenko maybe you can look into this?

  5. Hi @plotka,

    This is a limitation of the current type logic I’m afraid. You'll need to give TS a hint by explicitly typing the callback param.

    e.g.

    select: (data: number) => console.log(data)

    It's not quite as gung-ho as casting, and can still help you maintain type safety: if you were to give it a string type it will complain at you as expected. It just can’t do the inference upfront.

    Background: useQueries is using a recursive conditional type to manually unwrap the array and infer the different data types of each array item one at a time. It’s not very friendly with context sensitive parameters such as callback params, because the latter also requires TS to do multiple rounds of inference before it knows what is the right candidate. So it gets stuck down the wrong path. With useQuery it works, because there's no queries array that TS needs to make guesses about, so it can get a definite candidate for the data-type after a single pass.

    Another example in the repo tests

  6. TkDodo removed labelsbug

  7. TkDodo added labelswontfix

  8. Closed as known limitation.

  9. TkDodo closed this

Sign in to join this conversation.