Skip to main content

invariant

A runtime type-checking function that verifies whether the given condition is truthy, and throws an error if it is falsy.

This function supports TypeScript's type narrowing.


Code

🔗 View source code


Interface

typescript
function invariant(value: boolean, message: string): asserts value;

function invariant<T>(
value: T | null | undefined,
message: string
): asserts value is T;

Usage

Boolean Condition Check

typescript
import { invariant } from '@modern-kit/utils';

invariant(user.isAdmin, "Admin privileges are required");
// After this point, user.isAdmin is guaranteed to be true.

Narrowing null/undefined Types

typescript
import { invariant } from '@modern-kit/utils';

invariant(user, "User does not exist");
// After this point, user is guaranteed to be the User type.