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

  • New Features

    • Added new public result-variant types for query and infinite-query options to provide more specific TypeScript typings.
  • Improvements

    • Tightened type inference for select callbacks so inferred data types align more accurately with query function outputs.
  • Tests

    • Added type-focused regression tests ensuring select only changes inferred TData when its input matches the query function data type.

鉁忥笍 Tip: You can customize this high-level summary in your review settings.

Regression tests (main fail / PR pass)

Adds type-level regression coverage for useQueries and useSuspenseQueries ensuring we only infer TData from select when select's input matches TQueryFnData.

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

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: da3634e

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

Tightens TypeScript inference for select in useQueries and useSuspenseQueries (select now expects TQueryFnData), introduces Result variants that wrap option types with a DataTag-typed queryKey, and updates related tests and public type exports.

Changes

Cohort / File(s) Summary
Public exports
packages/react-query/src/index.ts
Adds six new exported result-type aliases for query and infinite-query options (re-exports).
Query options types
packages/react-query/src/queryOptions.ts
Adds DefinedInitialDataOptionsResult, UndefinedInitialDataOptionsResult, UnusedSkipTokenOptionsResult; overloads updated to return these Result types with queryKey as DataTag.
Infinite query options types
packages/react-query/src/infiniteQueryOptions.ts
Adds *InfiniteOptionsResult variants and updates infiniteQueryOptions overloads to return them, removing inline queryKey shapes.
useQueries typing
packages/react-query/src/useQueries.ts
Refactors conditional types to infer TData from select?: (data: TQueryFnData) => ... when present; else fall back to TQueryFnData.
useSuspenseQueries typing
packages/react-query/src/useSuspenseQueries.ts
Same tightening as useQueries: select parameter now constrained to TQueryFnData in conditional branches.
Type tests
packages/react-query/src/__tests__/useQueries.test-d.tsx, packages/react-query/src/__tests__/useSuspenseQueries.test-d.tsx
Refactors tests to declare queryKey, queryFn, derive TQueryFnData, inline queries with satisfies UseQueryOptions and add regression tests ensuring select only infers TData when input matches TQueryFnData.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • TkDodo
  • manudeli

Poem

🐰 I sniffed the types and gave a hop,
select now knows where data should drop.
From fuzzy any to TQueryFnData true,
Queries cuddle types — a tidy view! ✨

🚥 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
Title check ✅ Passed The title clearly and concisely summarizes the main change: improving the select function's type inference in useQueries.
Linked Issues check ✅ Passed All code changes directly address the linked objectives: improved select inference in useQueries/useSuspenseQueries (#6556) and Result type aliases to stabilize declaration output (#8453).
Out of Scope Changes check ✅ Passed All changes are within scope: type inference improvements, new Result type definitions, and corresponding type tests for validating the fixes.
Description check ✅ Passed The PR description comprehensively covers all required template sections with clear motivation, detailed changes, and proper testing information.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

Copy link
Collaborator

@TkDodo TkDodo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve checked both the useQueries and useSuspenseQueries test changes against the main branch and they also don’t yield any errors there.

To verify if your improvements are correct, please show test-cases that fail on main while yielding correct results on your PR. Thanks.

@ganyudedog
Copy link
Author

I’ve checked both the useQueries and useSuspenseQueries test changes against the main branch and they also don’t yield any errors there.

To verify if your improvements are correct, please show test-cases that fail on main while yielding correct results on your PR. Thanks.

@TkDodo ,I added type-level regression tests that are main-fail/PR-pass.

They assert we only infer TData from select when selects input matches TQueryFnData (so unrelated select signatures don't affect inferred results) for both useQueries (QueriesOptions/QueriesResults) and useSuspenseQueries (SuspenseQueriesOptions/SuspenseQueriesResults).

Verified with: pnpm -C packages/react-query test:types:tscurrent

Thanks.

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

2 participants