TanStack Query v5: Simplified API and Improved TypeScript Support

TanStack Query v5: Simplified API and Improved TypeScript Support

TanStack Query v5: Simplified API and Improved TypeScript Support

TanStack Query v5 is a major release of the popular server-state management library, simplifying the API surface while significantly improving TypeScript support.

Unified Object API

All hooks now accept a single options object — positional argument overloads have been removed:

// v4
useQuery(['todos'], fetchTodos, { staleTime: 5000 });

// v5
useQuery({ queryKey: ['todos'], queryFn: fetchTodos, staleTime: 5000 });

Improved TypeScript Inference

v5 infers the return type of queryFn automatically without requiring explicit generics in most cases. Error types are now unknown by default, encouraging proper error handling.

useMutationState for Optimistic Updates

const pendingPosts = useMutationState({
    filters: { mutationKey: ['addPost'], status: 'pending' },
    select: (mutation) => mutation.state.variables,
});

Migration Codemod

npx jscodeshift -t @tanstack/query-codemods/v5/rename-properties.js ./src
All Comments