这是我的代码:
let errorBadRequest = new Error("Bad request");
res.statusCode = 400;
errorBadRequest.errors = err.details.reduce(function(prev, current) {
prev[current.path] = current.message;
return prev;
}, {});
throw errorBadRequest;
我想在错误实例中扩展error
属性,但tsc说joi-utils.ts(21,23):错误TS2339:类型“Error”上不存在属性“errors”。
errors
的结构是{fieldname: fieldmsg}
,这是根据我的joi请求schema来决定的。
如何解决 typescript 编译器的错误?我想我需要声明一个接口(interface)并指定属性。
最佳答案
Property 'errors' does not exist on type 'Error'.
创建一个名为 error-extension.d.ts
的文件并包含以下内容:
interface Error {
errors: Error;
}
更多:https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html
关于node.js - 如何扩展 Error 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35261507/