fix(query-core): stop wrapping persister generics in NoInfer #10510
main fromrefs/pull/10510/head·merged by TkDodo View on GitHubSummary#
Fixes #7842.
QueryOptions.persister was typed as QueryPersister<NoInfer<TQueryFnData>, NoInfer<TQueryKey>, NoInfer<TPageParam>>, which prevented the persister from contributing to TQueryFnData inference. When the companion queryFn declared a parameter (e.g. (_context) => 'test'), TypeScript failed to infer TQueryFnData from its return and defaulted to unknown, producing a spurious overload mismatch against a concretely-typed persister.
Removing the NoInfer wrappers lets persister participate in inference. queryFn still drives inference when the types agree, and genuine conflicts between persister and queryFn still surface as errors (covered by a new negative type test in queryOptions.test-d.tsx).
Repro#
declare function createPersister<TData>(): QueryPersister<TData, any>
// Works (baseline)
queryOptions({
queryKey: ['a'],
queryFn: () => 'hello',
persister: createPersister<string>(),
})
// BEFORE: TS2769 "No overload matches this call"
// AFTER: compiles cleanly
queryOptions({
queryKey: ['b'],
queryFn: (_context) => 'hello',
persister: createPersister<string>(),
})
Verification#
packages/query-coretype tests: pass across TS 5.4, 5.8, 6.0 current (tsc --build tsconfig.legacy.json)packages/react-querytype tests: pass (new positive + negative cases inqueryOptions.test-d.tsx)packages/vue-query,packages/solid-query,packages/preact-querytype tests: pass- Genuine type conflicts (e.g.
queryFn: () => 42withpersister: QueryPersister<string>) still produce the expected overload mismatch — verified by the new@ts-expect-errorcases.
Changeset#
@tanstack/query-core: patch — type-only change, no runtime behaviour affected.
🤖 Generated with Ora Studio
Summary by CodeRabbit#
Bug Fixes
- Improved TypeScript inference for typed persisters so query functions that accept parameters no longer produce spurious overload mismatch errors, enhancing type safety and IDE experience for parameterized queries.
Tests
- Added type-level tests to confirm correct inference from persisters and to ensure genuine type conflicts remain reported.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core
🧹 Nitpick comments (1)
packages/react-query/src/__tests__/queryOptions.test-d.tsx (1)
303-319: Minor: silencevitest/expect-expectfor this type-only negative test.ESLint flags this test as having no assertions because
@ts-expect-erroris the implicit assertion. Either disable the rule locally or wrap the calls inassertType(...)to satisfy the linter.Proposed fix
- it('should still error when persister and queryFn return types genuinely conflict', () => { + // eslint-disable-next-line vitest/expect-expect + it('should still error when persister and queryFn return types genuinely conflict', () => { const persister = undefined as unknown as QueryPersister<string, any>🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/react-query/src/__tests__/queryOptions.test-d.tsx` around lines 303 - 319, This test is a type-only negative test using `@ts-expect-error` but triggers the vitest/expect-expect lint rule; either disable the rule locally for this block or add a no-op type assertion to make ESLint happy. Modify the test in packages/react-query/src/__tests__/queryOptions.test-d.tsx around the two queryOptions(...) calls: either add an inline ESLint disable comment for "vitest/expect-expect" scoped to the test, or wrap each call with a type-level helper such as assertType<unknown>(queryOptions(...)) (or another existing assertType utility) so the linter sees an assertion while preserving the `@ts-expect-error` checks on queryFn vs persister.🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Nitpick comments: In `@packages/react-query/src/__tests__/queryOptions.test-d.tsx`: - Around line 303-319: This test is a type-only negative test using `@ts-expect-error` but triggers the vitest/expect-expect lint rule; either disable the rule locally for this block or add a no-op type assertion to make ESLint happy. Modify the test in packages/react-query/src/__tests__/queryOptions.test-d.tsx around the two queryOptions(...) calls: either add an inline ESLint disable comment for "vitest/expect-expect" scoped to the test, or wrap each call with a type-level helper such as assertType<unknown>(queryOptions(...)) (or another existing assertType utility) so the linter sees an assertion while preserving the `@ts-expect-error` checks on queryFn vs persister.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
878349b8-d2be-4c9a-816a-c2d3de88ede1📥 Commits
Reviewing files that changed from the base of the PR and between dbee9ebdc1a242a64cd99156ab9298dae041f6d0 and 1546819aec582076bf373759f6f818f2741ab51a.
📒 Files selected for processing (3)
.changeset/fix-persister-infer-when-queryfn-has-parameter.mdpackages/query-core/src/types.tspackages/react-query/src/__tests__/queryOptions.test-d.tsx
Addressed the
vitest/expect-expectnitpick in cbc0246 — both negative cases are now wrapped inassertType(...). Types + ESLint both pass locally. Thanks for the review!View your CI Pipeline Execution ↗ for commit 85317f2c6ea372eed20b8c16984bce0fa97125c8
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 6m 7s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 1s View ↗
☁️ Nx Cloud last updated this comment at
2026-04-25 09:36:29UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-preact-simple
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-offline
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-eslint-plugin-demo
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-optimistic-updates-cache
- @tanstack/query-example-react-optimistic-updates-ui
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@10510@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@10510@tanstack/preact-query
npm i https://pkg.pr.new/@tanstack/preact-query@10510@tanstack/preact-query-devtools
npm i https://pkg.pr.new/@tanstack/preact-query-devtools@10510@tanstack/preact-query-persist-client
npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@10510@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@10510@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@10510@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@10510@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@10510@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@10510@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@10510@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@10510@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@10510@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@10510@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@10510@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@10510@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@10510@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@10510@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@10510@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@10510@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@10510@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@10510@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@10510commit:
85317f2TkDodo approved these changes
TkDodo merged thisea4497e
TkDodo closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
07a8672c-626b-4234-b1f1-0e49a05efb8f📥 Commits
Reviewing files that changed from the base of the PR and between cbc0246d35fdcf137359e47417c9bf4ac6831c01 and 9323f9859e08a8d15b7e6e896cb6e36025429093.
📒 Files selected for processing (1)
packages/query-core/src/types.ts✅ Files skipped from review due to trivial changes (1)
📝 Walkthrough
Walkthrough#
This PR removes
NoInfer<>wrappers from thepersisterproperty inQueryOptionsso a typedpersistercan participate in inferringTQueryFnDataeven whenqueryFndeclares parameters. Type-tests were added to assert correct inference and to assert real mismatches still error.Changes#
\.changeset/fix-persister-infer-when-queryfn-has-parameter.md@tanstack/query-coredocumenting the persister type-inference fix.packages/query-core/src/types.tsNoInfer<>wrappers fromQueryOptions.persister, changing its type toQueryPersister<TQueryFnData, TQueryKey, TPageParam>sopersistercan influenceTQueryFnDatainference.packages/react-query/src/__tests__/queryOptions.test-d.tsxpersisterwhenqueryFnhas a context/argument, and negative//@ts-expect-error`` tests verifying genuine type mismatches still fail.Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#7842: removed NoInfer wrappers from QueryPersister generics to allow persister participation in TQueryFnData inference, with comprehensive type test coverage ensuring both positive cases and genuine type conflicts are properly handled.#7842: changeset documentation, QueryOptions type definition update, and type test additions with no unrelated modifications.✨ Finishing Touches
🧪 Generate unit tests (beta)
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