自定义错误类扩展使用吸气剂的错误

我想创建自己的错误类,以扩展基本的JS Error类并定义多个获取器。

示例:

class CustomError extends Error {
  public get code(): string {
    return "my-code";
  }
}

const myError = new CustomError("uuu");

console.log("message",myError.message); // the message "uuu"
console.log("code",myError.code); // undefined

问题是,如果我想获取getter值,则会得到不确定的返回值,而不是所需的值。

有人知道这是为什么还是我做错了吗?

谢谢!

编辑:我的tsconfig.json

{
  "compilerOptions": {
    "target": "es5","module": "commonjs","lib": ["dom","es6","es2016"],"declaration": true,"outDir": "dist","rootDir": "src","removeComments": true,"strict": true,"moduleResolution": "node","esModuleInterop": true,},"exclude": [ "node_modules" ],"include": [ "src/**/*.ts" ]
}

请参阅repl.it:https://repl.it/repls/DentalVoluminousPerformance

LIUYAOZU 回答:自定义错误类扩展使用吸气剂的错误

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2953721.html

大家都在问