Skip to content

fix(react-query): preserve narrowing with NoInfer#11022

Open
Gujiassh wants to merge 1 commit into
TanStack:mainfrom
Gujiassh:fix/react-query-noinfer-union-narrowing-11018
Open

fix(react-query): preserve narrowing with NoInfer#11022
Gujiassh wants to merge 1 commit into
TanStack:mainfrom
Gujiassh:fix/react-query-noinfer-union-narrowing-11018

Conversation

@Gujiassh

@Gujiassh Gujiassh commented Jul 3, 2026

Copy link
Copy Markdown

🎯 Changes

  • wrap the useQuery return-type NoInfer fence in a distributive helper
  • preserve the reverse-inference guard that rejects incorrect contextual UseQueryResult annotations
  • restore discriminated-union narrowing on query.data
  • add focused dts coverage for the narrowing regression reported in #11018

Closes #11018

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.
  • Additional local validation: pnpm --filter @tanstack/react-query test:types:tscurrent

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).
  • This PR changes published React Query types. I have not added a changeset in this contribution.

@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR introduces a NarrowableNoInfer<T> type in useQuery.ts, a distributive conditional type wrapping NoInfer<T>, applied to TData across the three useQuery overload return types. A new type-level test verifies discriminated-union narrowing is preserved.

Changes

NarrowableNoInfer Fix

Layer / File(s) Summary
NarrowableNoInfer type and overload updates
packages/react-query/src/useQuery.ts
Adds NarrowableNoInfer<T> conditional type wrapping NoInfer<T> and applies it to TData in DefinedInitialDataOptions, UndefinedInitialDataOptions, and UseQueryOptions overload return types.
Discriminated-union narrowing type test
packages/react-query/src/__tests__/useQuery.test-d.tsx
Adds a new type test confirming query.data?.type === 'first' ? undefined : query.data narrows correctly to the 'second' variant or undefined.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • TanStack/query#10593: Prior change to useQuery's NoInfer typing that this PR fixes a regression from.
  • TanStack/query#10985: Related NoInfer-driven type-inference fix in a related query generics area.

Suggested labels: package: react-query

Suggested reviewers: TkDodo

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The code matches #11018 by wrapping NoInfer distributively, preserving the reverse-inference guard, and adding narrowing coverage.
Out of Scope Changes check ✅ Passed All changes are confined to useQuery typing and a related dts test, with no unrelated code introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly matches the main change: preserving narrowing while adjusting NoInfer behavior in react-query.
Description check ✅ Passed The description follows the template and covers changes, checklist, and release impact with enough detail.
✨ 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.

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.

useQuery return type NoInfer preserves reverse-inference guard but breaks discriminated-union narrowing

1 participant