Convex Type Helpers - TS2589 Fixes
The Problem
Error: "Type instantiation is excessively deep and possibly infinite" (TS2589)
Cause: Convex generates deeply nested types that exceed TypeScript's recursion limits when:
- •Using
useMutation(api.x.y)oruseQuery(api.x.y)directly - •Inline object literals in component function calls
- •Cross-module type inference chains
Solution: convexHelpers.ts
MUST exist at lib/convexHelpers.ts (or equivalent path):
typescript
import { api } from "@convex/_generated/api";
import { useMutation, useQuery, useAction } from "convex/react";
export const apiAny = api as any;
export function useConvexMutation(path: any) {
return useMutation(path);
}
export function useConvexQuery(path: any, args?: any) {
return useQuery(path, args);
}
export function useConvexAction(path: any) {
return useAction(path);
}
Usage
typescript
// BEFORE (broken)
import { api } from "@convex/_generated/api";
import { useMutation } from "convex/react";
const myMutation = useMutation(api.game.createDeck); // TS2589!
// AFTER (fixed)
import { apiAny, useConvexMutation } from "@/lib/convexHelpers";
const myMutation = useConvexMutation(apiAny.game.createDeck); // Works!
Quick Fix Checklist
- •Import from
@/lib/convexHelpers, neverconvex/reactdirectly - •Use
apiAny.module.functioninstead ofapi.module.function - •Extract inline objects to variables before passing to Convex calls
- •Add
skipLibCheck: trueto tsconfig if needed