【发布时间】:2020-10-10 21:40:17
【问题描述】:
我是 Typescript 的新手,我正在尝试在应用程序中个性化我的错误类。
我总共有四门课; Exception、GenericError、BaseService、CarService 两个主要和两个继承。 GenericError 扩展了 ExceptionClass 并且 CarService 扩展了 BaseService。我无法更改 GenericError 类的构造函数属性。我收到此错误;
这个表达式是不可构造的。
我的类继承逻辑有什么问题?
// Exception.ts
class ExceptionClass extends Error {
status: number;
message: string;
errors?: object;
constructor(status: number, message: string, errors?: object) {
super(message);
this.status = status;
this.message = message;
this.errors = errors;
}
}
// GenericError.ts
class GenericException extends ExceptionClass {
constructor(message?: string) {
super(400, message || 'Internal Error');
}
}
// BaseService.ts
class BaseService {
genericError: GenericException;
constructor(message: string) {
this.genericError = new GenericException();
}
}
// CarService.ts
export default class CarService extends BaseService {
constructor() {
super('')
console.log("CarService started!");
}
async checkCar() {
return new Promise(async (resolve: any, reject: any) => {
//** Imagine some business logic that fails */
const passwordsNotMatchErr = new this.genericError('Fail Reason bla bla');
return reject(passwordsNotMatchErr);
})
}
}
【问题讨论】:
-
只是提醒一下,将函数标记为异步然后返回一个承诺是没有意义的。
标签: javascript typescript oop