这是indexloc提供的服务,不要输入任何密码
Skip to content

useQueries does not infer correct type for select parameter – data is typed as unknown #9396

@Junnis0123

Description

@Junnis0123

Describe the bug

When using useQueries in React Query v5 with TypeScript, the select function’s input parameter data is inferred as unknown, even though the return type of queryFn should be known.

This leads to an unintuitive developer experience where data must be manually typed or casted, despite the fact that the shape of the data is already known via queryFn.

This does not happen in useQuery, which correctly infers the type of data passed to select.

Your minimal, reproducible example

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBATgUwgVwDZQjAvDZEECKyCcAlkgBQDeAUDDAI7FlIBcMA2nfTLTz0xIBPANIIh7DgHIEADwCGAWwAOqBFIC6AGm79BcIQDEw7CgEpsAPhgAFOCEWl8AOkQQQqAG4JqMeeykACwRUVBApLRgAI3YAFgAmGABfMx1+enw1YChTABN5KHkLLGt8wud5SIB6KphAGXIYMvkYJxgpZDAAazAQAHcwKV1k7g0aFIBuIA

Steps to reproduce

  1. Create a React component using useQueries
  2. Pass an array with a query using queryFn that returns an object
  3. Add a select function like (data) => data.a
  4. Hover data in an IDE or let TypeScript check its type
  5. Observe that data is inferred as unknown

Expected behavior

I expected the data parameter in the select function to be inferred based on the return type of queryFn, just like it is in useQuery. This would enable proper autocomplete and type checking without requiring manual type assertions or annotations.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS 14.5
  • IDE: VSCode

Tanstack Query adapter

None

TanStack Query version

v5.66.9

TypeScript version

5.2.2

Additional context

This issue breaks the type inference parity between useQuery and useQueries.

Similar issues have been raised in the past (#3994, #6556, #7270), but this problem remains in React Query v5 and may require a fresh look.

Even though the select function’s return type is inferred, the input data is not — likely because the UseQueryOptions<TQueryFnData, TError, TData> is defaulting to unknown when used in a dynamic array (useQueries).

A fix (or improved inference strategy) would significantly improve DX when using select with useQueries.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions