T
TanStack14mo ago
rare-sapphire

Using typescript - How do I set a custom type for useMutation?

I want my useMutation to contain it's regular mutate, inLoading and others but I also want it to contain percentage. How can I set the type?
interface LUejeczob2
extends Request<{ type?: MediaTypes }, null, { file: File }> {}

interface LftdYVO9fH
extends Response<{ url: string; thumbnail: string }> {}

export const uploadImage = (): UseMutationResult<
LftdYVO9fH,
Qasah.Response<any>,
LUejeczob2
> & { percentage: number } => {
const [percentage, setPercentage] = useState(0);

const mutation = useMutation({
mutationFn: async (payload: LUejeczob2) =>
await client.post('/upload', payload.body, {
params: payload.params,
headers: {
'Content-Type': 'multipart/form-data',
},
onUploadProgress: file => {
setPercentage(Math.round((file.loaded * 100) / file.total!));
},
}),
});

return { ...mutation, percentage };
};
interface LUejeczob2
extends Request<{ type?: MediaTypes }, null, { file: File }> {}

interface LftdYVO9fH
extends Response<{ url: string; thumbnail: string }> {}

export const uploadImage = (): UseMutationResult<
LftdYVO9fH,
Qasah.Response<any>,
LUejeczob2
> & { percentage: number } => {
const [percentage, setPercentage] = useState(0);

const mutation = useMutation({
mutationFn: async (payload: LUejeczob2) =>
await client.post('/upload', payload.body, {
params: payload.params,
headers: {
'Content-Type': 'multipart/form-data',
},
onUploadProgress: file => {
setPercentage(Math.round((file.loaded * 100) / file.total!));
},
}),
});

return { ...mutation, percentage };
};
This does not work, I get
No description
5 Replies
rare-sapphire
rare-sapphireOP14mo ago
Any help here please?
ambitious-aqua
ambitious-aqua14mo ago
Show a typescript playground please
rare-sapphire
rare-sapphireOP14mo ago
Already figured it out. Thank you
afraid-scarlet
afraid-scarlet14mo ago
Provide the solutions for others then please… that's the decent thing to do
rare-sapphire
rare-sapphireOP14mo ago
Thing is, the answer does not even make any sense to me, so I'm still trying to understand it before posting it. So I post with an explanation

Did you find this page helpful?