The problem is that Javascript's built-in class Error
breaks the prototype chain by switching the object to be constructed (i.e. this
) to a new, different object, when you call super
and that new object doesn't have the expected prototype chain, i.e. it's an instance of Error
not of CustomError
.
This problem can be elegantly solved using 'new.target', which is supported since Typescript 2.2, see here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html
class CustomError extends Error {
constructor(message?: string) {
// 'Error' breaks prototype chain here
super(message);
// restore prototype chain
const actualProto = new.target.prototype;
if (Object.setPrototypeOf) { Object.setPrototypeOf(this, actualProto); }
else { this.__proto__ = actualProto; }
}
}
Using new.target
has the advantage that you don't have to hardcode the prototype, like some other answers here proposed. That again has the advantage that classes inheriting from CustomError
will automatically also get the correct prototype chain.
If you were to hardcode the prototype (e.g. Object.setPrototype(this, CustomError.prototype)
), CustomError
itself would have a working prototype chain, but any classes inheriting from CustomError
would be broken, e.g. instances of a class VeryCustomError < CustomError
would not be instanceof VeryCustomError
as expected, but only instanceof CustomError
.
See also: https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200