[英]I Keep getting, 'Object is of type 'unknown'' error
I'm trying to use a switch statement to notify the client side of any errors我正在尝试使用 switch 语句来通知客户端任何错误
axios.interceptors.response.use(response => {
return response
}, (error: AxiosError) => {
const {data, status} = error.response!;
switch (status) {
case 400:
toast.error(data.title);
break;
case 401:
toast.error(data.title);
break;
case 500:
toast.error(data.title);
break;
default:
break;
}
return Promise.reject(error.response);
})
But it complains about the 'data' variable, saying 'Object is of type 'unknown'.但它抱怨'数据'变量,说'对象是'未知'类型。 After doing some research they said I have to use a type guard, but I don't believe type guards are available for switch statements.
在做了一些研究之后,他们说我必须使用类型保护,但我不相信类型保护可用于 switch 语句。 Does anyone know how to rewrite this as an if statement or knows what I can do to fix this?
有谁知道如何将其重写为 if 语句或知道我能做些什么来解决这个问题?
Does marking it as optional and defaulting the value get rid of the error?将其标记为可选并默认值会消除错误吗?
const {data? = null, status} = error.response!;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.