如何定义Axios错误响应的类型
我打电话给API。我可以从Axios Response.Data
中收到两种响应类型。如果是200。 iindustrygrouplist
如果它的另一个错误(如400
export const GetIndustry = async () => {
const base = Config.api_url;
const endpoint = "/api/industry";
const url = base + endpoint;
try {
const response = await HttpClient.get<IIndustryGroupList[]>(url, options);
return [response, null] as const;
} catch (err: any) {
// here we can receive the axios response for a 400.
return [null, err.response] as const;
}
};
。代码> iapierRorresponse 。更具体地说 axios.Response.data
应为类型 iapierRorresponse
用法
const [response, error] = await GetIndustry();
// response.data of type IIndustryGroupList
// error.data of type IAPIErrorResponse
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您要实现的目标,但这肯定会给您达到想要的东西:
TSPlayground
I'm not reealy sure of what you are trying to achieve, but this will definitely give you the means to reach what you want:
TSPlayground
您可以按照以下方式进行操作:
You can do it in following way: