It is a known issue :
instanceof is broken when class extends Error type related to
to the Polymer standard support with TypeScript feature.
Proposed workarounds are :
- creating an intermediary class
- setting the prototype
Unfortunately this is a change that we made to try to try to adopt a
more standard-compliant emit so that we could enable Polymer to work
with TypeScript.
For background, was an intentional change in 2.2 (see #12123 and the
section on our wiki), but is difficult to overcome through
compilation. I believe there's some conversation in #12790 for
workarounds.
A workaround you can take now is create an intermediate class that you
can extend from.
export interface MyErrorStatic {
new (message?: string): RxError;
}
export interface MyError extends Error {}
export const MyError: MyErrorStatic = function MyError(this: Error, message: string) {
const err = Error.call(this, message);
this.message = message;
this.stack = err.stack;
return err;
} as any;
export class HttpError extends MyError {
// ...
}
In TypeScript 2.2, you'll be able to set the prototype on your own.
// Use this class to correct the prototype chain.
export class MyError extends Error {
__proto__: Error;
constructor(message?: string) {
const trueProto = new.target.prototype;
super(message);
// Alternatively use Object.setPrototypeOf if you have an ES6 environment.
this.__proto__ = trueProto;
}
}