Skip to content

fix(query-core): setQueryData updater keeps discriminated-union members when spreading a narrowed value - #11915

Open
boriskozak wants to merge 1 commit into
TanStack:mainfrom
boriskozak:query-11795-w1
Open

boriskozak wants to merge 1 commit into
TanStack:mainfrom
boriskozak:query-11795-w1

Conversation

@boriskozak

@boriskozak boriskozak commented Oct 4, 2026 •

Copy link
Copy Markdown

🎯 Changes

setQueryData typed the updater's input as NoInfer<TInferredQueryFnData> | undefined. With NoInfer in the input position, a narrowed discriminated union lost its variant-specific fields when spread inside the updater, so this failed with TS2345:

client.setQueryData(query.queryKey, (prev) => {
  if (!prev || prev.kind !== 'a') return prev;
  return { ...prev, value: prev.value + 1 }; // TS2345 before this change
});

The fix drops NoInfer from the updater's input position (TInferredQueryFnData | undefined) while keeping it on the return position and the method's return type, so inference from the tagged query key still flows the same way. One-line type change in queryClient.ts, plus a type-level regression test and a changeset.

Also fixes #11794 (same root cause).

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr (see notes below).
  • I have followed the AI contribution policy and fully understand the code in this pull request, including any code generated with AI assistance.

pnpm run test:pr notes: 134 tasks passed, including all test:types targets (TS 5.6 through 7.0 across affected packages), test:eslint, test:build, and build. Five failures are unrelated to this change: query-devtools:test:lib and eslint-plugin-query:test:lib pass on re-run (flaky under parallel nx); angular-query-experimental:test:lib fails identically on clean main (Angular TestBed environment issue: "Cannot set base providers because it has already been called"); root:test:knip and root:test:docs fail because this checkout is missing examples/** and integrations/**.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.

Fixes #11795

Summary by CodeRabbit

  • Bug Fixes
    • Fixed type inference for setQueryData updater functions, allowing discriminated union data to be narrowed and updated while accounting for possibly undefined data.

@boriskozak boriskozak changed the title fix(query-core): setQueryData updater keeps discriminated-union members when spreading a narrowed value 🤖🤖🤖 fix(query-core): setQueryData updater keeps discriminated-union members when spreading a narrowed value Oct 4, 2026
@changeset-bot

changeset-bot Bot commented Oct 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 45540d1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 24 packages
Name Type
@tanstack/query-core Patch
@tanstack/angular-query-experimental Patch
@tanstack/lit-query Patch
@tanstack/preact-query Patch
@tanstack/query-async-storage-persister Patch
@tanstack/query-broadcast-client-experimental Patch
@tanstack/query-devtools Patch
@tanstack/query-persist-client-core Patch
@tanstack/query-sync-storage-persister Patch
@tanstack/react-query Patch
@tanstack/solid-query Patch
@tanstack/svelte-query Patch
@tanstack/vue-query Patch
@tanstack/preact-query-devtools Patch
@tanstack/preact-query-persist-client Patch
@tanstack/react-query-devtools Patch
@tanstack/solid-query-devtools Patch
@tanstack/svelte-query-devtools Patch
@tanstack/vue-query-devtools Patch
@tanstack/react-query-persist-client Patch
@tanstack/solid-query-persist-client Patch
@tanstack/svelte-query-persist-client Patch
@tanstack/react-query-next-experimental Patch
@tanstack/eslint-plugin-query Patch

Not sure what this means? Click here to learn what changesets are.

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

@coderabbitai

coderabbitai Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: TanStack/query/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0e464ed6-bd20-4dec-86cd-257767d93448
📥 Commits

Reviewing files that changed from the base of the PR and between eaa75f4 and 45540d1.

📒 Files selected for processing (3)
  • .changeset/tidy-cougars-serve.md
  • packages/query-core/src/__tests__/queryClient.test-d.tsx
  • packages/query-core/src/queryClient.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

setQueryData no longer applies NoInfer to the updater input type. A type regression test checks discriminated-union narrowing and spreading, and a patch changeset records the update.

Changes

setQueryData updater typing

Layer / File(s) Summary
Updater typing and regression coverage
packages/query-core/src/queryClient.ts, packages/query-core/src/__tests__/queryClient.test-d.tsx, .changeset/tidy-cougars-serve.md
The updater input type uses TInferredQueryFnData | undefined directly. The regression test checks that narrowing preserves variant-specific fields when spreading the value. The changeset records a patch release for @tanstack/query-core.

Priority: ➖ Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: sukvvon

Merge Risk: ⚪ Minimal · up to 45540

This compile-time API improvement supports tagged-union narrowing in setQueryData without an identified merge-blocking risk.

Security Architecture Review

Security architecture risk: ⚪ Minimal · up to 45540

The change improves callback typing without changing runtime cache access, update behavior, or authority. No material security risk introduced or worsened by this PR was identified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The newly accepted TypeScript callback expressions use the existing cache-update capability; they do not gain a new runtime entrypoint or access to a different client cache. This conclusion is limited to the reviewed declaration change, not application-specific cache isolation.

Trust Boundaries and Controls

  • inferred — NoInfer governs TypeScript inference rather than runtime authorization or validation. Removing it from the input position does not remove an executable security control; existing callers already supply query keys and updater functions to the same implementation.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Issues #11794 and #11795 require setQueryData to preserve discriminated-union narrowing and variant fields when the updater spreads a narrowed value, while retaining tagged-key inference. `queryClie…
Out of Scope Changes check ✅ Passed The changes are limited to the setQueryData updater input type, a regression test for the reported union-narrowing behavior, and a patch changeset for @tanstack/query-core. Each change supports is…
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Title check ✅ Passed The title clearly describes the main change: preserving discriminated-union members in a narrowed setQueryData updater. It is specific, though somewhat long.
Description check ✅ Passed The description explains the change and motivation, completes the checklist, and identifies the release impact and changeset. It also reports test results and known failures.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

1 participant