@quentinadam/assert
v0.1.18
Published
A simple assertion function
Readme
@quentinadam/assert
A simple assertion function.
Usage
import { assert, AssertionError } from '@quentinadam/assert';
assert(1 + 1 === 2); // passes
try {
assert(1 + 1 === 3);
} catch (e) {
// throws AssertionError
}
try {
assert(1 + 1 === 3, 'Math is broken');
} catch (e) {
// throws AssertionError with custom message
}
try {
assert(1 + 1 === 3, new Error('Math is broken'));
} catch (e) {
// throws a custom Error
}
const value: string | undefined = 'hello';
assert(value !== undefined); // narrows the type of value to string
value.toUpperCase(); // works