fix(react-query): preserve Mutation generics in useMutationState select callback#10464
fix(react-query): preserve Mutation generics in useMutationState select callback#10464mukeshdhadhariya wants to merge 1 commit intoTanStack:mainfrom
Conversation
📝 WalkthroughWalkthroughUpdated Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/react-query/src/useMutationState.ts`:
- Around line 25-31: Update the generic defaults to preserve type precision:
import DefaultError from "@tanstack/query-core" and change the TError default
from unknown to DefaultError, and change the TResult default from the bare
MutationState to a parameterized MutationState<TData, TError, TVariables,
TContext> across the three declarations mentioned (MutationStateOptions,
getResult, and useMutationState) so that provided
TData/TError/TVariables/TContext flow into the result type and improve inference
for mutation.state.error in select callbacks.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 4c87af88-2335-4204-936f-7b25bdae9309
📒 Files selected for processing (1)
packages/react-query/src/useMutationState.ts
| type MutationStateOptions< | ||
| TData = unknown, | ||
| TError = unknown, | ||
| TVariables = unknown, | ||
| TContext = unknown, | ||
| TResult = MutationState | ||
| > = { |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "== query-core defaults =="
rg -n --type=ts -C2 'export interface MutationState<|export class Mutation<' packages/query-core/src/mutation.ts
echo
echo "== react adapter defaults =="
rg -n --type=ts -C2 'type MutationStateOptions<|function getResult<|export function useMutationState<' packages/react-query/src/useMutationState.tsRepository: TanStack/query
Length of output: 560
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== Full MutationStateOptions signature ==="
sed -n '25,35p' packages/react-query/src/useMutationState.ts
echo
echo "=== Full getResult signature ==="
sed -n '38,60p' packages/react-query/src/useMutationState.ts
echo
echo "=== Full useMutationState signature ==="
sed -n '62,75p' packages/react-query/src/useMutationState.ts
echo
echo "=== Current imports ==="
sed -n '1,15p' packages/react-query/src/useMutationState.tsRepository: TanStack/query
Length of output: 1543
Default generic parameters regress type precision and break generic composition.
At lines 27, 40, and 64, TError = unknown diverges from query-core's TError = DefaultError, weakening type inference for mutation.state.error in select callbacks.
At lines 30, 43, and 67, TResult = MutationState (bare) is not parameterized. When callers provide TData, TError, TVariables, and TContext generics, they don't flow into the default return shape, breaking type safety for composed usage.
Apply the suggested fix across all three declarations (MutationStateOptions, getResult, useMutationState):
- Import
DefaultErrorfrom@tanstack/query-core - Change
TErrordefault fromunknowntoDefaultError - Change
TResultdefault fromMutationStatetoMutationState<TData, TError, TVariables, TContext>
Suggested type-level fix
import type {
+ DefaultError,
Mutation,
MutationCache,
MutationFilters,
MutationState,
QueryClient,
} from '@tanstack/query-core'
type MutationStateOptions<
TData = unknown,
- TError = unknown,
+ TError = DefaultError,
TVariables = unknown,
TContext = unknown,
- TResult = MutationState
+ TResult = MutationState<TData, TError, TVariables, TContext>
> = {
filters?: MutationFilters
select?: (
mutation: Mutation<TData, TError, TVariables, TContext>
) => TResult
}
function getResult<
TData = unknown,
- TError = unknown,
+ TError = DefaultError,
TVariables = unknown,
TContext = unknown,
- TResult = MutationState
+ TResult = MutationState<TData, TError, TVariables, TContext>
>(
mutationCache: MutationCache,
options: MutationStateOptions<
TData,
TError,
TVariables,
TContext,
TResult
>,
): Array<TResult> {
export function useMutationState<
TData = unknown,
- TError = unknown,
+ TError = DefaultError,
TVariables = unknown,
TContext = unknown,
- TResult = MutationState
+ TResult = MutationState<TData, TError, TVariables, TContext>
>(Also applies to: lines 38–44, 62–68
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/react-query/src/useMutationState.ts` around lines 25 - 31, Update
the generic defaults to preserve type precision: import DefaultError from
"@tanstack/query-core" and change the TError default from unknown to
DefaultError, and change the TResult default from the bare MutationState to a
parameterized MutationState<TData, TError, TVariables, TContext> across the
three declarations mentioned (MutationStateOptions, getResult, and
useMutationState) so that provided TData/TError/TVariables/TContext flow into
the result type and improve inference for mutation.state.error in select
callbacks.
not true. it’s still |
Summary
#10463 issue
useMutationStatecurrently loses type information in theselectcallback becauseMutationis used without generics. This causes values likemutation.state.datato be inferred asunknown.Changes
MutationStateOptionsgenericMutationin theselectcallbackgetResultanduseMutationStateto support genericsExample
Before:
const result = useMutationState({
select: (mutation) => mutation.state.data
})
// result: unknown[]
After:
const result = useMutationState({
select: (mutation) => mutation.state.data
})
// result: number[]
Impact
Motivation
This ensures that type information from core
Mutationis preserved in the React adapter, improving developer experience when usingselect.Summary by CodeRabbit