Skip to content

Instantly share code, notes, and snippets.

View TomRadford's full-sized avatar

Tom Radford TomRadford

View GitHub Profile
@TomRadford
TomRadford / createOptimisticMutation.ts
Created May 25, 2026 15:02
Optimistic mutation util for the real ones 🤝
import type { QueryKey, UseQueryOptions } from "@tanstack/react-query";
import { mutationOptions } from "@tanstack/react-query";
import { queryClient } from "./clients";
type AnyQueryOptions = UseQueryOptions<any, any, any, any>;
type InferQueryData<TFactory extends () => AnyQueryOptions> =
ReturnType<TFactory> extends UseQueryOptions<infer TData, any, any, any> ? TData : never;
type InferQueryError<TFactory extends () => AnyQueryOptions> =
ReturnType<TFactory> extends UseQueryOptions<any, infer TError, any, any> ? TError : never;