@se-oss/assert is a lightweight, type-safe assertion library for TypeScript and JavaScript, powered by @se-oss/is.
npm install @se-oss/assertInstall using your favorite package manager
pnpm
pnpm install @se-oss/assertyarn
yarn add @se-oss/assert@se-oss/assert provides a set of assertion functions that throw a TypeError if the condition is not met. It is powered by @se-oss/is, so all predicates from is are available as assertions.
import { assert } from '@se-oss/assert';
// Throws if not a string
assert.string('hello');
// Throws with a custom message
assert.string(123, 'Value must be a string');
// => TypeError: Value must be a string// Numbers
assert.number(42);
assert.integer(42);
assert.positiveNumber(10);
assert.inRange(15, 'Not in range', [10, 20]);
// Objects & Arrays
assert.plainObject({ foo: 'bar' });
assert.array([1, 2, 3]);
assert.nonEmptyArray([1]);
// Strings
assert.nonEmptyString('hello');
assert.json('{"a":1}');
assert.ipv4('127.0.0.1');
// Logic
assert.truthy(true);
assert.falsy(false);The library is fully type-safe and provides type guards for your variables.
function process(value: unknown) {
assert.string(value);
// 'value' is now narrowed to string
console.log(value.toUpperCase());
}For all configuration options, please see the API docs.
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub
Thanks again for your support, it is much appreciated! π
MIT Β© Shahrad Elahi and contributors.