我想创建自己的错误类,以扩展基本的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