npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@crutchcrew/invariant

v1.1.1

Published

Yet another tiny invariant package for your TypeScript project

Downloads

646

Readme

invariant 🔬🔨

npm version gzip size coverage provenance license

TypeScript invariant with custom error class support — tiny as tiny-invariant, type-safe as ts-invariant, versatile as nothing else.

How it works

An invariant function takes a value and throws if the value is falsy. If the value is truthy, execution continues and TypeScript narrows the type.

import { invariant } from "@crutchcrew/invariant"

const user: User | null = getUser()
invariant(user, "User not found")
console.log(user.name) // user is narrowed to User

Why this package

| | This package | tiny-invariant | ts-invariant | invariant | | --------------------------------------- | ------------ | --------------------------------------------------------------- | ------------------------------------------------------------------- | ------------------------------------------------- | | Size (gzip) | ~364 B | ~370 B | ~1.0 kB | ~1.1 kB | | Zero Dependencies | ✅ | ✅ | ❌ | ❌ | | Tree-shakeable ESM | ✅ | ✅ | ✅ | ❌ | | Type narrowing | ✅ | ✅ | ✅ | ❌ | | Strict typing | ✅ | ❌ | ❌ | ❌ | | Lazy messages | ✅ | ❌ | ❌ | ❌ | | Console methods | ✅ | ❌ | ✅ | ❌ | | Invariant factory | ✅ | ❌ | ❌ | ❌ |

Install

Choose your fighter 🥊

pnpm add @crutchcrew/invariant
bun add @crutchcrew/invariant
yarn add @crutchcrew/invariant
npm add @crutchcrew/invariant

Usage

const response = await fetch("/users/1")
invariant(response.ok, `Request failed: ${response.status}`)

const user = await response.json()

Lazy messages

Pass a function to defer message construction and avoid expensive message computation

invariant(value, getExpensiveMessage)

Custom errors

If you need to throw domain-specific errors — a NotFoundError, a ValidationError, or anything else — you're left wrapping calls or rolling your own helper. This package provides createInvariant to build an invariant function that throws any error class you give it, with the same assertion narrowing and lazy message support.

Use createInvariant to throw your own error type:

import { createInvariant } from "@crutchcrew/invariant"

class HttpError extends Error {
	name = "HttpError"
}

const invariant = createInvariant(HttpError)

export async function getUser(id: string) {
	const response = await fetch(`/users/${id}`)
	invariant(response.ok, `Request failed: ${response.status}`)

	return response.json()
}

Console methods

The invariant function exposes debug, log, warn, and error methods that delegate to the corresponding console methods:

invariant.debug(`Message ${id} sent`)
invariant.log("User", user)
invariant.warn("Unexpected state", { detail })
invariant.error("Something went wrong")

Strict typing

The default invariant allows to pass any value as a condition so it can check on any truthy/falsy value — objects, strings, numbers, whatever you hand it. Import from @crutchcrew/invariant/strict instead to require an actual boolean, catching accidental truthy/falsy checks at the type level:

import { invariant } from "@crutchcrew/invariant/strict"

invariant(user !== null, "User not found") // ✅ boolean condition
invariant(user, "User not found") // ❌ type error: User | null is not assignable to boolean

It's the same runtime as the regular invariant — just a stricter type layer — so createInvariant and InvariantError are also available from /strict.

API

invariant(condition, message?)

// default
(condition: unknown, message?: string | (() => string)) => asserts condition
// strict
(condition: boolean, message?: string | (() => string)) => asserts condition

Throws InvariantError if condition is falsy. Narrows the type of condition to truthy.

createInvariant(ErrorClass)

;<E extends Error>(ErrorClass: new (message: string) => E) => Invariant

Returns a new invariant function that throws ErrorClass instead of InvariantError.

InvariantError

Default error class thrown by invariant. Extends Error with framesToPop = 1 for cleaner stack traces.

Development

bun install
bun test
bun run check    # fmt + lint (with type-check) in parallel

Credits

The API and InvariantError design are based on ts-invariant by Ben Newman and the Apollo team. The /strict entry point is inspired by ts-tiny-invariant by Igor Yegoroff.