简体   繁体   English

我不断收到,“对象的类型为‘未知’”错误

[英]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.

相关问题 该程序有效,但我收到错误 Object is of type 'unknown'.ts(2571) at the bottom cart[1] - The program works but I'm getting an error Object is of type 'unknown'.ts(2571) at the bottom cart[1] 打字稿错误对象的类型为“未知” - Typescript Error Object is of type 'unknown' 不断收到“对象不是函数”错误 - Keep getting 'Object is not a function' error 我得到一个类型:未知错误,说它不能在 React/Typescript 上输入 - I am getting a type: Unknown Error saying it not assignable to type on React/Typescript React:如何解决错误:Object is of type 'unknown'? - React: How to solve the error: Object is of type 'unknown'? 在反应应用程序上工作并继续收到错误 Expected `onChange` listener to be a function,而不是得到了 `object` 类型的值 - Working on react app and keep on getting the error Expected `onChange` listener to be a function, instead got a value of `object` type errorObj:未知 Object 属于“未知”类型 - errorObj: unknown Object is of type 'unknown' 未知类型:代码 || 将此错误作为模式错误理智地获取 - Unknown type: code || getting this error in sanity as schema error 我不断收到此错误。 类型错误:addItems 不是 function。 (在 'addItems(text)' 中,'addItems' 是 Object 的一个实例) - I keep getting this error. TypeError: addItems is not a function. (In 'addItems(text)', 'addItems' is an instance of Object) 对象的类型为“未知”.ts - Object is of type 'unknown'.ts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM