T
TanStack2y ago
genetic-orange

Typing mutate() variables when using setMutationDefault

Hi, I have some simple code here to set up a default mutation function and then call that with a useMutation, but it doesn't like the variables object I am passing in to mutate because it's not of type void. How do I type the variables object? The mutation function parameters are already typed so I'm not sure where it's expecting me to set this up.
// query setup
const createAction = async ({
activityId,
startTime,
nickname,
}: ICreateActionParam) => {
... // basic try catch API call
}

queryClient.setMutationDefaults(["createAction"], {mutationFn: createAction})

// in react component
const createActionMutation = useMutation({ mutationKey: ["createAction"] })

const onAddAction = () => {
// ts error: Argument of type '{ activityId: number; startTime: string; nickname: string; }' is not assignable to parameter of type 'void
createActionMutation.mutate({
activityId: Number(activityId),
startTime: new Date().toISOString(),
nickname: nickname,
})
}
// query setup
const createAction = async ({
activityId,
startTime,
nickname,
}: ICreateActionParam) => {
... // basic try catch API call
}

queryClient.setMutationDefaults(["createAction"], {mutationFn: createAction})

// in react component
const createActionMutation = useMutation({ mutationKey: ["createAction"] })

const onAddAction = () => {
// ts error: Argument of type '{ activityId: number; startTime: string; nickname: string; }' is not assignable to parameter of type 'void
createActionMutation.mutate({
activityId: Number(activityId),
startTime: new Date().toISOString(),
nickname: nickname,
})
}
1 Reply
genetic-orange
genetic-orangeOP2y ago
figured it out
const createActionMutation = useMutation<unknown, Error, ICreateActionParam>({ mutationKey:
["createAction"] })
const createActionMutation = useMutation<unknown, Error, ICreateActionParam>({ mutationKey:
["createAction"] })

Did you find this page helpful?