feat(useMutationState): add TMutation generic to propagate mutation types into select callback#10790
Conversation
Add a second generic parameter `TMutation extends Mutation<any, any, any, any>` to `MutationStateOptions` and `useMutationState` across all framework adapters. This lets callers type the `select` callback's mutation argument without manual casting. Fixes: users having to write `mutation.state as MutationState<MyData, MyError, MyVars>` when filtering by a known mutationKey. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughAdds a ChangesuseMutationState TMutation Generic Propagation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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. Comment |
Adding a new generic parameter to a public API is a new feature, not a bug fix. The feat() prefix in the description already signals this. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…pters Documents the caller-side assertion semantics of TMutation so users understand the mutation cache stores base Mutation types and the generic is their responsibility to keep consistent with filters. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Summary
Fixes #9825
The
mutationargument in theselectcallback ofuseMutationStatealways had typeMutation<unknown, Error, unknown, unknown>, losing any specific type information and requiring manual casts.Before
After
Changes
TMutation extends Mutation<any, any, any, any> = Mutationgeneric toMutationStateOptionsanduseMutationStatein all framework adapters:react-queryvue-querysolid-querysvelte-querypreact-querylit-queryreact-queryverifying thatTMutationgenerics propagate correctly into theselectcallbackMutationso existing usage is fully backward compatibleType safety note
TMutationis a caller-side assertion.mutationCache.findAll()always returns the baseMutationtype, so the castmutation as unknown as TMutationis type-only and erased at runtime. It is the caller's responsibility to ensureTMutationmatches the actual mutations in the cache (e.g. by also specifyingmutationKeyinfilters).Test plan
npx nx run @tanstack/react-query:test:lib— all runtime tests passnpx nx run @tanstack/react-query:test:types— type tests pass on TS 5.4 through 6.0useMutationState.test-d.tsx— "should propagate TMutation generics into select callback"🤖 Generated with Claude Code