T
TanStack3y ago
genetic-orange

Type the api response in useMutation and Usequery

export interface LoginSuccessRes {
access_token: string
success: boolean
token_type: string
}

export interface LoginFailed {
result: string
success: boolean
details: string
}


const navigate = useNavigate()
const { mutateAsync, data: loginresponse } = useMutation<LoginSuccessRes,LoginFailed>({
mutationFn: loginUser,
})

I was expecting a type matching MutationFunction<LoginSuccessRes, void>, but instead you passed (logindetails: { email: string; password: string; }) => Promise<any>

what is the right way to type?
export interface LoginSuccessRes {
access_token: string
success: boolean
token_type: string
}

export interface LoginFailed {
result: string
success: boolean
details: string
}


const navigate = useNavigate()
const { mutateAsync, data: loginresponse } = useMutation<LoginSuccessRes,LoginFailed>({
mutationFn: loginUser,
})

I was expecting a type matching MutationFunction<LoginSuccessRes, void>, but instead you passed (logindetails: { email: string; password: string; }) => Promise<any>

what is the right way to type?
5 Replies
genetic-orange
genetic-orange3y ago
Just infer…
genetic-orange
genetic-orangeOP3y ago
@M00LTi can you provide a sample code?
genetic-orange
genetic-orange3y ago
const { mutateAsync, data: loginresponse } = useMutation({
mutationFn: loginUser,
})
const { mutateAsync, data: loginresponse } = useMutation({
mutationFn: loginUser,
})
The type will automatically be inferred
genetic-orange
genetic-orangeOP3y ago
understood basically type loginUser function and rest will be taken care of noice @M00LTi thanks mate
export async function loginUser(logindetails: {
email: string
password: string
}): Promise<LoginResponse> {}
export async function loginUser(logindetails: {
email: string
password: string
}): Promise<LoginResponse> {}
if someone wants to know how here how
rare-sapphire
rare-sapphire3y ago
useMutation will infer type from your mutateFn please check typescript promise function return type

Did you find this page helpful?