我想将主机对象扩展Error
到自定义UploadError
类。以下示例在我编译时失败:
class UploadError extends Error {
constructor(message: string, private code: number) {
super(message);
}
getCode(): number {
return this.code;
}
}
当我运行 TypeScript 编译器时tsc
,出现以下错误:
UploadError.ts(1,0): A export class may only extend other classes, Error is an interface.
它似乎Error
被定义为一个接口。如果有人知道实现的名称是什么,那会让我非常高兴:-)
更新:我想使用 Typescripts 继承,而不是像我目前用来解决这个问题的原型继承:
function UploadError (message: string, code: number) {
this.message = message;
this.code = code;
}
UploadError.prototype = new Error();
UploadError.prototype.constructor = UploadError;
UploadError.prototype.getCode = (): number => {
return this.code;
}