在Typescript to version 4.4配置添加useUnknownInCatchVariables属性默认值true

try {
    ...
} catch(e) {
    console.log(e.message) //报错Object is of type 'unknown'
}

解决

try {
    ...
} catch(e) {
    console.log((e as Error).message)
}
or:

try {
    ...
} catch(e) {
    if (e instanceof Error) {
        console.log(e.message)
    }
}

或者在 tsconfig.json更改如下

{
    "compilerOptions": {
        "strict": true,
        "useUnknownInCatchVariables": false
    }
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注