I would like to extend the host object Error to a custom UploadError class. The following example fails when I compile:
class UploadError extends Error {
constructor(message: string, private code: number) {
super(message);
}
getCode(): number {
return this.code;
}
}
When I run the TypeScript compiler tsc I get the following error:
UploadError.ts(1,0): A export class may only extend other classes, Error is an interface.
It seems Error is defined as an interface. If anyone knows what the name of the implementation is it would make me very happy 🙂
Update: I want to use Typescripts inheritance not prototypical inheritance like I currently employ to hack around this:
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;
}
I have found the following approach works:
The generated script looks like: