TypeScript assertion functions.
- Statement form (
invariant) and expression form (nonnull) - Narrows types
- Throws a typed error class (
InvariantError) - Supports lazy messages — only evaluated on failure
- Clean stack traces
- Works in browser and on the server
- Zero dependencies
deno add jsr:@quad/invariantFor other runtimes:
bunx jsr add @quad/invariant # bun
npx jsr add @quad/invariant # npm
pnpm add jsr:@quad/invariant # pnpm
yarn add jsr:@quad/invariant # yarnimport { invariant, nonnull } from "@quad/invariant";
const node: Node = getNode();
invariant(node instanceof HTMLElement, "expected an element"); // `node` narrowed to `HTMLElement`
const parent: HTMLElement = nonnull(node.parentElement, "expected a parent");import { invariant, nonnull } from "@quad/invariant";
const result: Success | Failure = await rpc.call(req);
invariant(result.ok, "rpc failed"); // `result` narrowed to `Success`
const token: string = nonnull(
result.session?.token,
"expected a session token",
);invariant(condition, message?) — assert condition is truthy, narrow via
asserts condition.
const value: string | number = parse(input);
invariant(typeof value === "string", "expected a string");
value.toUpperCase(); // `value` narrowed to `string`nonnull<T>(value, message?) — assert value is non-null, narrow and
return it.
const name: string = nonnull(user?.name, "expected a name");message: string | (() => string) — lazy form only evaluates on failure.
invariant(condition, () => `expected ${expensive()}`);InvariantError — an exported Error subclass, thrown by both. Stack trace
points at the call site, not the library internals.
try {
invariant(condition);
} catch (e) {
if (e instanceof InvariantError) reportBug(e);
throw e;
}See PRIOR_ART.md for a comparison of 20+ existing libraries and the design decisions behind this one.
Copyright 2026 Substrate AI, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.