Typescript instanceof在我的API网关上不起作用

时间:2019-01-21 19:29:39

标签: node.js typescript api serverless

我有此功能可用于管理Typescript无服务器API网关上的错误

export abstract class ErrorResult extends Error {
  public constructor(public code: string, public description: string) {
    super(description);
  }
}

export class BadRequestResult extends ErrorResult {}

export class ConfigurationErrorResult extends ErrorResult {}

export class ForbiddenResult extends ErrorResult {}

export class InternalServerErrorResult extends ErrorResult {}

export class NotFoundResult extends ErrorResult {}

我正在做这个

let error: NotFoundResult = new NotFoundResult('UNKNOWN_USER', 'There is no user with the specified ID!');
          if(error instanceof NotFoundResult) {
            console.log('WORK!!!');
          }

但是没有显示消息,我怎么了?

1 个答案:

答案 0 :(得分:0)

  

但未显示消息

它显示很好。这是示例代码以及可运行的演示:

abstract class ErrorResult extends Error {
    public constructor(public code: string, public description: string) {
        super(description);
    }
}
class NotFoundResult extends ErrorResult { }
let error: NotFoundResult = new NotFoundResult(
    'UNKNOWN_USER',
    'There is no user with the specified ID!'
);
if (error instanceof NotFoundResult) {
    console.log('WORK!!!');
}

enter image description here