is-kit

assert

Assert-style helper for fail-fast validation. When the guard passes, the value stays narrowed for the rest of the scope.

Assert with Guard

Throws when the guard fails; otherwise continues with a narrowed value.

import { assert, isString } from 'is-kit';
declare const input: unknown;
assert(isString, input, 'input must be a string');
// input is narrowed to string here
input.toUpperCase();

Assert with define

Build a reusable guard with define, then assert once to fail fast and continue with a narrowed type.

import { assert, define, isBoolean, isString, struct } from 'is-kit';
type User = { id: string; active: boolean };
type ActiveUser = User & { active: true };
const isUser = struct({
id: isString,
active: isBoolean,
});
const isActiveUser = define<ActiveUser>(
(value) => isUser(value) && value.active === true
);
declare const input: unknown;
assert(isActiveUser, input);
// input is narrowed to ActiveUser here
input.active; // true