TanStackT
TanStack2y ago
3 replies
primary-violet

useMutation Error and Type Safe

Please Help!!! I have tried many tricks to make this work but the error keeps coming

export const formSchema = z
  .object({
    email: z
      .string({
        required_error: 'Email is required',
        invalid_type_error: 'Name must be a string'
      })
      .email({ message: 'Please Provide a valid email' }),
    firstName: z.string().min(3, { message: 'First Name Must be valid' }),
  })
  .required()

// Since the function does not return any data it is void
export async function createClient(
  values: z.infer<typeof formSchema>
): Promise<void> {
  return await api.post('/client', values)
}

const { mutate } = useMutation(createClient, { // Type '(values: { email: string; firstName: string;}) => Promise<void>' has no properties in common with type 'UseMutationOptions<unknown, Error, void, unknown>'.ts(2559)
  onSuccess: () => {
    queryClient.invalidateQueries({ queryKey: ['clients'] })
  }
})

const onSubmit = async (values: z.infer<typeof formSchema>) => {
  await mutate(values) // Argument of type '{ email: string; firstName: string;}' is not assignable to parameter of type 'void'.
}
Was this page helpful?