1

这是我的代码:

  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): error TS2339: Property 'errors' does not exist on type 'Error'.

的结构errors{fieldname: fieldmsg},是根据我的joi request schema来决定的。

如何解决打字稿编译器的错误?我想我需要声明一个接口并指定属性。

4

2 回答 2

1

“错误”类型不存在属性“错误”。

创建一个名为error-extension.d.ts并具有以下内容的文件:

interface Error {
    errors: Error;
} 

更多:https ://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html

于 2016-02-08T02:36:48.323 回答
0

我发现在初始化Error课程时,实际上它并没有errorsError. 它应该创建一个界面并设置errors为选项。

这是我的解决方案:

interface IJoiErrorException extends NodeJS.ErrnoException {
  errors?: Object;
}

const errorBadRequest: IJoiErrorException = new Error("Bad request");
于 2016-02-08T02:49:10.873 回答