must-be-err
v0.1.1
Published
Error type assertion for TypeScript 4.4+ unknown errors in `catch` clause
Downloads
7
Readme
must-be-err
Error type assertion for TypeScript 4.4+ unknown errors in catch
clause.
Installation
npm i must-be-err
Usage
import mustBeErr from 'must-be-err';
try {
// ...
} catch (err) {
// `err` is unknown in TypeScript 4.4+
mustBeErr(err); // It throws if `err` is not an `Error`.
// `err` is now an `Error`.
console.error(err.message);
}
const notAnError = 123;
mustBeErr(notAnError);
// Error: The argument `err` is not an `Error`, got 123.
Source
export default function mustBeErr(err: unknown): asserts err is Error {
if (err instanceof Error === false) {
throw new Error(`The argument \`err\` is not an \`Error\`, got "${err}".`);
}
}