Skip to content

Instantly share code, notes, and snippets.

@sophistifunk
Created April 21, 2018 04:42
Show Gist options
  • Save sophistifunk/e8db29e2aca3f70ec1a649df0cbd38fd to your computer and use it in GitHub Desktop.
Save sophistifunk/e8db29e2aca3f70ec1a649df0cbd38fd to your computer and use it in GitHub Desktop.
class Foo extends Error {
type: string;
constructor(type: string, message: string) {
super(message);
// Clean up the changes made by super()
this.constructor = Foo;
if ((Object as any).setPrototypeOf) {
(Object as any).setPrototypeOf(this, Foo.prototype);
} else {
(this as any).__proto__ = Foo.prototype;
}
// Only reference props on this after this point for safety
this.type = type;
}
doStuff() {
return "I am a " + this.type + " type of error."
}
}
let e = new Foo("dramatic", "Goodbye, cruel world");
console.clear();
console.log('e instanceof Error', e instanceof Error);
console.log('e instanceof Foo ', e instanceof Foo);
console.log('e.type ', e.type);
console.log('typeof e.doStuff ', typeof e.doStuff);
console.log('');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment