TypeScript Base Extendible Error
NPM: https://www.npmjs.com/package/ts-base-error
Usage Example
index.ts
import BaseError from 'ts-base-error'; class Err1 extends BaseError {}const e1 = new Err1();console.log(e1 instanceof Err1); // trueconsole.log(e1 instanceof Error); // true class Err2 extends Err1 {}const e2 = new Err2();console.log(e2 instanceof Err1); // trueconsole.log(e2 instanceof Err2); // trueconsole.log(e2 instanceof Error); // true class NoBaseErr extends Error {}const x = new NoBaseErr();console.log(x instanceof Error); // trueconsole.log(x instanceof NoBaseErr); // false !!!
Test
npx ts-node test.ts
Problems
Class constructor error
- Error message:
Class constructor BaseError cannot be invoked without 'new'
. - Solution: Set target to
ES6
. Ref: https://stackoverflow.com/a/50203532/717267