我想做一个类型的axios响应依赖于状态代码:
类型:
type SuccessResponseData = {
users: ....
};
interface SuccessResponse
extends AxiosResponse<SuccessResponseData, any> {
status: 200;
}
type FailResponseData = {
message: string;
};
interface FailResponse extends AxiosResponse<FailResponseData, any> {
status: 400;
}
export type Response = SuccessResponse | FailResponse;
字符串
这就是我如何使用它:
const response = await axios
.post<SuccessResponse, AxiosResponse<SuccessResponse>>(
endpoint,
data,
)
.catch((e: AxiosError<FailResponse>) => {
return e.response;
});
return response as Response;
型
但响应与响应不兼容
1条答案
按热度按时间xoefb8l81#
尝试在.then中使用“SuccessResponse”作为响应,而不是直接对帖子进行响应。
“.post”可以返回成功或失败,然后点击“.then”或“.catch”