Skip to content

fix(react-query): enforce TData consistency when select is not used#10844

Closed
raashish1601 wants to merge 1 commit into
TanStack:mainfrom
raashish1601:fix/react-query-usequery-data-type-guard
Closed

fix(react-query): enforce TData consistency when select is not used#10844
raashish1601 wants to merge 1 commit into
TanStack:mainfrom
raashish1601:fix/react-query-usequery-data-type-guard

Conversation

@raashish1601
Copy link
Copy Markdown
Contributor

@raashish1601 raashish1601 commented May 30, 2026

Summary

  • Add useQuery overloads in
    eact-query that enforce TData extends TQueryFnData when select is not provided.
  • Preserve current select behavior via separate overload so transformed result types remain supported.
  • Add a type regression test in useQuery.test-d.tsx for the mismatch case.

Validation

  • Focused TypeScript check:
    ode ../../node_modules/typescript59/lib/tsc.js --pretty false --noEmit --strict --skipLibCheck --moduleResolution bundler --module ESNext --target ES2020 --jsx react-jsx src/useQuery.ts src/tests/useQuery.test-d.tsx

Summary by CodeRabbit

  • Tests

    • Added TypeScript compile-time validation tests for useQuery to ensure type compatibility with the select parameter.
  • Chores

    • Refined TypeScript type signatures for useQuery to better distinguish between uses with and without the select parameter.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 58422699-896a-4f7c-912d-b27630b51025

📥 Commits

Reviewing files that changed from the base of the PR and between 7fa2781 and d798d1c.

📒 Files selected for processing (2)
  • packages/react-query/src/__tests__/useQuery.test-d.tsx
  • packages/react-query/src/useQuery.ts

📝 Walkthrough

Walkthrough

This PR refactors useQuery TypeScript overloads to more strictly enforce generic type constraints when a select option is or is not provided. Helper types distinguish "with select" versus "without select" configurations, overloads are updated to use the appropriate variant, and tests validate the resulting type-checking behavior.

Changes

useQuery Select Option Type Safety

Layer / File(s) Summary
Helper types and base overload rewiring
packages/react-query/src/useQuery.ts
New UseQueryOptionsWithSelect and UseQueryOptionsWithoutSelect helper types enforce whether select is present or omitted; the first two useQuery overloads are wired to use the WithoutSelect variant.
Select-specific overload
packages/react-query/src/useQuery.ts
The third useQuery overload is updated to require UseQueryOptionsWithSelect, which enforces a non-undefined select function.
Type safety validation tests
packages/react-query/src/__tests__/useQuery.test-d.tsx
Local Post type and two test cases validate that incompatible generics without select trigger a compile-time error, and that the same generics compile when select transforms the result.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Select with care, or watch the types align,
Two paths now split—with select, or without the line.
The overloads learn which transforms they may take,
And tests stand guard to catch each GenericType mistake.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive The description covers the main changes and includes validation details, but lacks completion of the required template sections (Checklist and Release Impact). Complete the PR description template by checking off applicable items in the Checklist and Release Impact sections to confirm testing and changeset requirements.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: enforcing TData consistency when select is not used in react-query's useQuery function.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@TkDodo TkDodo closed this May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants