@quentinadam/ensure
v0.1.2
Published
A simple function that asserts that a value is not undefined or null and returns the narrowed value
Readme
@quentinadam/ensure
A simple function that asserts that a value is not undefined or null and returns the narrowed value.
Usage
import ensure from '@quentinadam/ensure';
const a = ensure(1); // doesn't throw
const b = ensure(undefined); // throws an AssertionError
ensure(undefined, 'message'); // throws an AssertionError with a custom error message
ensure(undefined, new Error('message')); // throws a custom Error
const value: string | undefined = 'hello';
const narrowedValue = ensure(value); // narrows the type of value to string;
value.toUpperCase(); // works