Skip to content
Source
Sign inSign up
TanStack/query

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

ClosedGujiassh(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/11022/headopened View on GitHub

🎯 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.
    1. 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 nameStatusExplanation
      Linked Issues check✅ PassedThe code matches #11018 by wrapping NoInfer distributively, preserving the reverse-inference guard, and adding narrowing coverage.
      Out of Scope Changes check✅ PassedAll changes are confined to useQuery typing and a related dts test, with no unrelated code introduced.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      Title check✅ PassedThe title clearly matches the main change: preserving narrowing while adjusting NoInfer behavior in react-query.
      Description check✅ PassedThe 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.
    2. i would rather just revert and remove the NoInfer from the return type. we don’t do this anywhere else and the bug that it fixed is questionable at best.

    3. TkDodo closed this

Sign in to join this conversation.