在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
}
}