如何访问 Error.code 属性? 我收到 Typescript 错误,因为类型“错误”上不存在属性“代码”。
this.authCtrl.login(user, {
provider: AuthProviders.Password,
method: AuthMethods.Password
}).then((authData) => {
//Success
}).catch((error) => {
console.log(error); // I see a code property
console.log(error.code); //error
})
最佳答案
真正的问题是 Node.js 定义文件没有导出正确的错误定义。它使用以下错误(并且不导出它):
interface Error {
stack?: string;
}
它导出的实际定义在 NodeJS 命名空间中:
export interface ErrnoException extends Error {
errno?: number;
code?: string;
path?: string;
syscall?: string;
stack?: string;
}
所以下面的类型转换会起作用:
.catch((error: NodeJS.ErrnoException) => {
console.log(error);
console.log(error.code);
})
这似乎是 Node 定义中的一个缺陷,因为它与 new Error() 中的对象实际包含的内容不一致。 TypeScript 将强制执行接口(interface)错误定义。
关于angular - 类型 'code' 上不存在属性 'Error',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141005/