TanStackT
TanStack17mo ago
7 replies
sacred-rose

How to properly type data in useMutation?

Hello everyone! I am using Tanstack React Query v4.36.1. Please help me to type this correctly? I don't understand why it's not working!
I'm using type AxiosResponse<User> and I can see that the correct data is being returned. But it still results in the following error:

No overload matches this call.
  The last overload gave the following error.
    Object literal may only specify known properties, and 'mutationKey' does not exist in type 'readonly unknown[]'. ts(2769)

useMutation.d.ts(6, 25): The last overload is declared here.

(property) mutationKey? MutationKey | undefined


Here is my code:

interface User {
  id: string
  firstName: string
  lastName: string
}

const fetchUser = (id: string) => axios.get<User>(`/user/${id}`)

const FetchUser = () => {
  const { data, mutate } = useMutation<AxiosResponse<User>>({
    mutationKey: ['fetchUser'],
    mutationFn: (id: string) => fetchUser(id),
  })

return <>...</>
}
Was this page helpful?