-
Hey. I'm working with an API that is typed in a way that it can return either a result or an error. What I struggle with is figuring out how to call Any ideas? Here is a minimal example: import { useMutation } from '@tanstack/react-query';
declare function get(): Promise<Result>;
type Result = {
id: string;
error?: never;
} | {
id?: never;
error: number
}
export const useCreate = () => {
return useMutation<string, number>({
mutationFn: async () => {
const result = await get();
return result.id;
// ^ type error since this can be undefined
},
onSuccess: (result) => {
// how to call this only if the result's error is undefined
}
});
}; |
Beta Was this translation helpful? Give feedback.
Answered by
TkDodo
Sep 18, 2024
Replies: 1 comment 1 reply
-
you need to return a rejected promise (or throw an error) in the |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
todor-a
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you need to return a rejected promise (or throw an error) in the
mutationFn