Skip to content

Conversation

@ganyudedog
Copy link

@ganyudedog ganyudedog commented Jan 18, 2026

Summary

This PR addresses two TypeScript typing issues in @tanstack/react-query:

Changes

  • Improves the inferred-options path in useQueries and useSuspenseQueries so TQueryFnData is derived from the queryFn return type and TData from the select output, restoring correct select(data) contextual typing.
  • Refactors queryOptions / infiniteQueryOptions overload return types to named *Result aliases to make declaration output more stable.
  • Updates type tests to assert inference without requiring explicit select parameter annotations.

Tests

  • pnpm -C packages/react-query run test:types:tscurrent

Fixes #6556.
Addresses #8453.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added new public result variant types for both query options and infinite query options, offering more specific TypeScript type selections for query configuration.
  • Improvements

    • Improved type inference for query select callbacks with stricter type constraints and enhanced accuracy in type resolution.

✏️ Tip: You can customize this high-level summary in your review settings.

Fixes TanStack#6556.

Also refactors queryOptions/infiniteQueryOptions overload return types to named *Result aliases to reduce TS4023 'cannot be named' issues for exported options objects (see TanStack#8453).
@changeset-bot
Copy link

changeset-bot bot commented Jan 18, 2026

⚠️ No Changeset found

Latest commit: 787714f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 18, 2026

📝 Walkthrough

Walkthrough

This PR improves TypeScript type inference for the select function in useQueries and useSuspenseQueries hooks to match useQuery behavior. It introduces new Result types using DataTag for queryKey type safety in queryOptions and infiniteQueryOptions, and updates conditional type logic to narrow select callback parameters from any to TQueryFnData.

Changes

Cohort / File(s) Summary
Type Declarations & Public API
packages/react-query/src/index.ts
Adds 6 new public type exports: DefinedInitialDataOptionsResult, UndefinedInitialDataOptionsResult, UnusedSkipTokenOptionsResult (for queryOptions) and their *InfiniteOptions variants. Re-exports from respective modules.
Query Options Result Types
packages/react-query/src/queryOptions.ts
Introduces three new *Result types that wrap base option types by omitting queryKey and redefining it as a DataTag. Updates function overloads to return DefinedInitialDataOptionsResult, UnusedSkipTokenOptionsResult, and UndefinedInitialDataOptionsResult instead of base types.
Infinite Query Options Result Types
packages/react-query/src/infiniteQueryOptions.ts
Introduces three new *InfiniteOptionsResult types with the same DataTag pattern. Updates infiniteQueryOptions function overloads to return the new Result variants, removing explicit queryKey properties from return shapes.
Hook Type Inference — useQueries
packages/react-query/src/useQueries.ts
Refines conditional type logic in GetUseQueryOptionsForUseQueries and GetUseQueryResult to check for select?: (data: TQueryFnData) => infer TData pattern instead of any, tightening type inference when select is present.
Hook Type Inference — useSuspenseQueries
packages/react-query/src/useSuspenseQueries.ts
Updates GetUseSuspenseQueryOptions and GetUseSuspenseQueryResult to replace select?: (data: any) with select?: (data: TQueryFnData), narrowing the callback parameter type in conditional branches.
Type Tests
packages/react-query/src/__tests__/useQueries.test-d.tsx, packages/react-query/src/__tests__/useSuspenseQueries.test-d.tsx
Refactor test definitions to use shared constants (queryKey, queryFn, TQueryFnData) and inline second query objects with satisfies UseQueryOptions constraints, improving type assertion coverage and clarity.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

package: react-query

Suggested reviewers

  • TkDodo
  • manudeli

Poem

🐰 Our types now dance with graceful care,
The select function finds its proper pair,
From any mists to TQueryFnData light,
The queries sing in harmony so bright! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main objective of the PR—improving type inference for the select callback in useQueries, which aligns with the core changes in useQueries.ts and useSuspenseQueries.ts.
Linked Issues check ✅ Passed The PR successfully addresses #6556 by improving select callback inference in useQueries and useSuspenseQueries, and partially addresses #8453 by exporting new *Result type aliases to stabilize declaration output.
Out of Scope Changes check ✅ Passed All changes are scoped to addressing the linked issues: type inference improvements in useQueries/useSuspenseQueries (direct fix for #6556), and new *Result type exports (addressing #8453).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type checking select works differently on useQueries compared to useQuery

1 participant