Skip to content

fix(vue-query): preserve branded types in MaybeRefDeep - #10573

Open
byungsker wants to merge 5 commits into
TanStack:mainfrom
byungsker:fix/vue-query-options-querykey
Open

byungsker wants to merge 5 commits into
TanStack:mainfrom
byungsker:fix/vue-query-options-querykey

Conversation

@byungsker

@byungsker byungsker commented Apr 25, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Changes

This PR fixes a TypeScript type inference issue where branded types (e.g. string & { __brand: 'PostId' }) in queryKey tuples cause TS2769 errors when passed from queryOptions to useQuery.

The root cause was MaybeRefDeep<T> recursively mapping branded primitive types as objects, which destroyed the branded type information needed for DataTag-based overload resolution.

The fix makes only primitive values terminal in MaybeRefDeep (T extends Primitive), so branded primitive types pass through unchanged while ordinary objects continue to recursively accept refs.

Regression coverage includes direct and nested branded query keys, plus an ordinary object with a __brand property and a nested ref.

Fixes #9920

Review follow-up

This PR incorporates the feedback from @peterbud:

  • Replaced the broad T extends { __brand: infer _ } terminal condition with T extends Primitive, so an ordinary object with a __brand property is still recursively unwrapped.
  • Added a focused type regression test for { __brand: 'post', postId: ref('123') }.
  • The implementation is in 7d972a00e6c8, and the focused regression-test follow-up is in de74b2e77.
  • The nested-ref regression now tests MaybeRefDeep directly instead of coupling it to a separate DataTag/useQuery inference shape.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Verification

  • pnpm nx run @tanstack/vue-query:test:types --skip-nx-cache — passed.
  • pnpm nx run @tanstack/vue-query:test:lib --skip-nx-cache — passed for Vue 2, Vue 2.7, and Vue 3: 28 test files, 319 passed, 6 skipped, no type errors.
  • pnpm nx run @tanstack/vue-query:test:eslint --skip-nx-cache — passed with 0 errors; 5 existing cspell warnings.
  • pnpm exec prettier --check packages/vue-query/src/types.ts packages/vue-query/src/__tests__/queryOptions.test-d.ts .changeset/branded-types-fix.md — passed.
  • pnpm run test:pr was also run against current main; the changed Vue Query targets passed, while the full run stopped at vue-declaration-emit:test:types (@babel/types unavailable locally) and root:test:knip (generated .tsup declaration outputs). Neither failure involves files changed by this PR.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed TypeScript inference issues when using branded types, such as branded strings, inside queryKey tuples passed through queryOptions and useQuery.
    • Preserved branded primitive types during deep reactive type mapping, including branded values nested in objects and references.
    • Improved type accuracy for query results involving branded identifiers.
  • Tests
    • Added TypeScript declaration tests covering direct and nested branded values, query options, reactive references, and inferred query results.

@coderabbitai

coderabbitai Bot commented Apr 25, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 929e1fa9-baae-424e-8a75-1f4c75d26d0f

📥 Commits

Reviewing files that changed from the base of the PR and between a0d8cfb and de74b2e.

📒 Files selected for processing (3)
  • .changeset/branded-types-fix.md
  • packages/vue-query/src/__tests__/queryOptions.test-d.ts
  • packages/vue-query/src/types.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • .changeset/branded-types-fix.md
  • packages/vue-query/src/types.ts
  • packages/vue-query/src/tests/queryOptions.test-d.ts

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


📝 Walkthrough

Walkthrough

This PR updates MaybeRefDeep to preserve branded primitive types during recursive mapping. Type tests cover branded values in direct and nested queryKey tuples. A patch changeset documents the fix.

Changes

Branded queryKey inference

Layer / File(s) Summary
Branded type handling and validation
packages/vue-query/src/types.ts, packages/vue-query/src/__tests__/queryOptions.test-d.ts
MaybeRefDeep now returns primitive types unchanged. Type tests verify branded values through direct, nested, inline, and queryOptions-based useQuery usage.
Release metadata
.changeset/branded-types-fix.md
Adds a patch changeset for @tanstack/vue-query describing the branded-type inference fix.

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

Merge Risk: ⚪ Minimal · up to de74b

This change updates Vue Query type inference so branded primitive query-key values can flow from queryOptions into useQuery without overload errors. No current merge-blocking risk is identified.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address issue #9920 by preserving branded primitive types in MaybeRefDeep, preventing the reported TS2769 error when queryOptions results are passed to useQuery. The added type tests cover…
Out of Scope Changes check ✅ Passed The modified implementation, regression tests, and changeset all support the linked issue and stated objectives. No unrelated code changes are identified.
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 and concisely identifies the primary change: preserving branded types in MaybeRefDeep for Vue Query.
Description check ✅ Passed The description includes the change, motivation, root cause, regression coverage, issue reference, checklist, release impact, changeset information, and verification results. The unchecked AI-assistan…
Full details: Linked Issues check

Explanation

The changes address issue #9920 by preserving branded primitive types in MaybeRefDeep, preventing the reported TS2769 error when queryOptions results are passed to useQuery. The added type tests cover direct, nested, and helper-function scenarios.

Full details: Docstring Coverage

Explanation

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 files. (1 skipped: 1 unsupported.)

Full details: Description check

Explanation

The description includes the change, motivation, root cause, regression coverage, issue reference, checklist, release impact, changeset information, and verification results. The unchecked AI-assistance acknowledgment is non-critical because the description is otherwise complete.

✨ 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.

@nx-cloud

nx-cloud Bot commented Apr 25, 2026 •

Copy link
Copy Markdown

🤖 Nx Cloud AI Fix Eligible

An automatically generated fix could have helped fix failing tasks for this run, but Self-healing CI is disabled for this workspace. Visit workspace settings to enable it and get automatic fixes in future runs.

To disable these notifications, a workspace admin can disable them in workspace settings.


View your CI Pipeline Execution ↗ for commit e261806

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ❌ Failed 2m 23s View ↗
nx run-many --target=build --exclude=examples/*... ❌ Failed 32s View ↗

☁️ Nx Cloud last updated this comment at 2026-08-22 14:22:06 UTC

@pkg-pr-new

pkg-pr-new Bot commented Apr 25, 2026 •

Copy link
Copy Markdown
More templates

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@10573

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@10573

@tanstack/preact-query

npm i https://pkg.pr.new/@tanstack/preact-query@10573

@tanstack/preact-query-devtools

npm i https://pkg.pr.new/@tanstack/preact-query-devtools@10573

@tanstack/preact-query-persist-client

npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@10573

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@10573

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@10573

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@10573

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@10573

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@10573

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@10573

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@10573

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@10573

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@10573

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@10573

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@10573

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@10573

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@10573

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@10573

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@10573

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@10573

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@10573

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@10573

commit: df73bb9

Branded types (string & { __brand: string }) were being recursively mapped
by MaybeRefDeep, causing type inference failures when used in queryKey
tuples passed from queryOptions to useQuery.

Fixes TanStack#9920
@byungsker
byungsker force-pushed the fix/vue-query-options-querykey branch from df73bb9 to af629b6 Compare July 22, 2026 01:44
@peterbud

Copy link
Copy Markdown
Contributor

Hi @byungsker , I quickly looked at this and the fix preserves branded primitive types, but it also treats any object with a __brand property as a terminal value. That regresses deep ref support for valid structured query keys such as { __brand: 'post', postId: ref('123') }, where postId is no longer accepted. I know it might be a rare case, but still...

I think narrowing the terminal condition to branded primitive intersections (T extends Primitive), or use a brand detection approach that does not exempt normal objects from recursive unwrapping would be a better solution. Also adding a regression type test for an object containing both __brand and a nested ref would be beneficial.

@byungsker

Copy link
Copy Markdown
Contributor Author

Good catch. The __brand check matched regular objects, so nested refs could be left untouched.

I changed it to T extends Primitive and added a type test for an object with __brand and a nested ref. The type tests pass. I'll push the fix.

@coderabbitai

coderabbitai Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Branded queryKey in queryOptions causes TS error (TS2769) in useQuery

3 participants