fix(types): add TMutation generic to useMutationState with caller-side assertion JSDoc#10870
fix(types): add TMutation generic to useMutationState with caller-side assertion JSDoc#10870junjuny0227 wants to merge 2 commits into
Conversation
…ters Adds a TMutation generic parameter to useMutationState (and equivalents in all six adapters) so the select callback receives a properly-typed Mutation instead of the base type. A MutationTypeFromResult<TResult> helper infers TMutation automatically from TResult when TResult is a typed MutationState, keeping the API fully backward-compatible. Also adds a @template TMutation JSDoc block on the exported hook function in every adapter so IDEs surface the caller-side assertion warning on hover — the only approach confirmed to work via the TypeScript Language Service API (inline JSDoc on type params is not shown at call sites). Closes TanStack#10792 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR adds a TMutation generic and a MutationTypeFromResult inference helper to useMutationState across React, Vue, Solid, Svelte, Preact, and Lit adapters, retyping MutationStateOptions and useMutationState signatures so select receives a narrowed mutation type, with JSDoc documenting the caller-side assertion requirement. ChangesuseMutationState TMutation Type Narrowing
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
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 |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/lit-query/src/useMutationState.ts (1)
177-220:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winMerge the two adjacent JSDoc blocks so IntelliSense doesn’t shadow docs
TypeScript’s language service only uses the immediately preceding JSDoc block for documentation/typing, so the inserted
@templateblock prevents the earlier@param/@returns/@exampledocs from showing on hover. Fold the@templatelines into the first JSDoc block.📝 Proposed fix (merge into the existing block)
* `@returns` An accessor for the selected mutation state array. + * + * `@template` TResult - The type of values returned by the `select` callback. + * `@template` TMutation - Narrows the type of the `mutation` argument passed to + * `select`. This is a caller-side assertion — the mutation cache stores + * mutations as the base `Mutation` type, so it is the caller's responsibility + * to ensure `TMutation` matches the actual mutations in the cache (e.g. by + * specifying a `mutationKey` in `filters`). * * `@example` * ```ts @@ * } * ``` */ -/** - * `@template` TResult - The type of values returned by the `select` callback. - * `@template` TMutation - Narrows the type of the `mutation` argument passed to - * `select`. This is a caller-side assertion — the mutation cache stores - * mutations as the base `Mutation` type, so it is the caller's responsibility - * to ensure `TMutation` matches the actual mutations in the cache (e.g. by - * specifying a `mutationKey` in `filters`). - */ export function useMutationState<🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/lit-query/src/useMutationState.ts` around lines 177 - 220, Merge the two adjacent JSDoc blocks so the `@template` tags are part of the main documentation for useMutationState: move the `@template` TResult and `@template` TMutation lines into the preceding JSDoc block that contains the `@param/`@returns/@example so the full comment immediately precedes the export function useMutationState declaration and IntelliSense shows the complete docs when hovering.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@packages/lit-query/src/useMutationState.ts`:
- Around line 177-220: Merge the two adjacent JSDoc blocks so the `@template` tags
are part of the main documentation for useMutationState: move the `@template`
TResult and `@template` TMutation lines into the preceding JSDoc block that
contains the `@param/`@returns/@example so the full comment immediately precedes
the export function useMutationState declaration and IntelliSense shows the
complete docs when hovering.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 99755d61-1e5c-4566-9237-0c6fa7c42595
📒 Files selected for processing (8)
.changeset/dog-brown-fox.mdpackages/lit-query/src/useMutationState.tspackages/preact-query/src/useMutationState.tspackages/react-query/src/useMutationState.tspackages/solid-query/src/useMutationState.tspackages/svelte-query/src/types.tspackages/svelte-query/src/useMutationState.svelte.tspackages/vue-query/src/useMutationState.ts
🎯 Changes
Closes #10792.
Problem
useMutationStateusesas unknown as TMutationinternally, which is a caller-side type assertion with no runtime validation. The safety warning for this existed only on theMutationStateOptionstype parameter — a location users rarely hover over in their IDE. As confirmed in the issue comment via the TypeScript Language Service API, inline JSDoc on a type parameter is not surfaced at the call site. Only a@templatetag in a function-level JSDoc block is shown on hover.Solution
Two changes applied across all six adapters (
react,preact,solid,vue,svelte,lit):1. Add
TMutationgeneric parameterA
MutationTypeFromResult<TResult>helper infersTMutationautomatically whenTResultis a typedMutationState, so theselectcallback receives the correctly-typedMutationwithout any manual casting:The default value of
TMutationfalls back to the baseMutationtype whenTResultis not a typedMutationState, preserving full backward compatibility.2. Surface the caller-side assertion warning via
@templateJSDocThis is the only approach confirmed to show the warning on hover at the call site (see issue for TypeScript Language Service API verification).
✅ Checklist
pnpm run test:pr.🚀 Release Impact
Summary by CodeRabbit