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

@mangs/assertions

v1.4.0

Published

TypeScript-based assertion functions to impose invariants at runtime

Readme

NPM Package @mangs/assertions Package Version

TypeScript-based assertion functions to impose invariants at runtime

Overview

JavaScript does not have native support for assertions, but TypeScript does enable assertion functions with a special asserts condition return syntax. This library uses the latter to create its concise syntax for writing invariants intended to guarantee characteristics of a command with precondition and postcondition assertions; the Hoare triple is the formalized form of this concept.

Ensuring Correct Behavior

Runtime assertions are a useful tool to verify expected behavior within an application. Rather than relying on complex, ahead-of-design-time quality mechanisms like design by contract, strong typing and well-placed assertions on their own can be a capable, lower effort substitute. Specifically, a real contract is established with at least 2 assertions checking each condition: 1 at the call site and 1 at the definition site. The article It Takes Two to Contract offers a great description for this duo's importance:

Besides readability, another pragmatic reason to pair assertions is robustness and defense in depth: as code is evolving and preconditions are strengthened or relaxed over time, having a separate set of assertions at each call site helps to ensure that refactoring around one call site doesn't break the other.

The rough idea here is to compute the same condition in multiple ways - one for each use case - and assert that each is identical, thus ensuring correct behavior and protecting against mistakes during future refactors.

For example, to assert that the number of posts added to a database never exceeds the length of the provided post array, we can do the following:

function insertPosts(posts: Post[]) {
  const rowsAdded = db.insert(posts);
  assert(rowsAdded <= posts.length);
  return rowsAdded;
}

const rowsAdded = insertPosts(postList);
assert(rowsAdded <= postList.length);

Type Narrowing With Less Code

Typically, TypeScript requires a conditional statement and possibly a thrown error to properly narrow a type. Instead, assertion functions are a more concise way to accomplish the same thing without any nesting. For example:

stringOrNumber; // Type: string | number
process.env.SOME_THING; // Type: string | undefined

assert(typeof stringOrNumber === "number");
assertNotNullish(process.env.SOME_THING);

stringOrNumber; // Type: number
process.env.SOME_THING; // Type: string

The alternative without assertion functions might look like this:

stringOrNumber; // Type: string | number
process.env.SOME_THING; // Type: string | undefined

if (typeof stringOrNumber !== "number") {
  throw new TypeError();
}
if (process.env.SOME_THING === undefined) {
  throw new TypeError();
}

stringOrNumber; // Type: number
process.env.SOME_THING; // Type: string

As you can see, using this library's assertion functions provides type narrowing in a more concise syntax and shows the code's true intent more clearly.

Inspirations

  • It Takes Two to Contract by Alex Kladov at TigerBeetle
    • This is a really excellent article detailing how a strong type system and proper assertions can be used in a complex codebase to ensure rock solid reliability
  • TypeScript assertion functions
    • For some reason this is buried in the TypeScript 3.7 release notes (see the link above) and can't be found easily in the documentation (it's barely mentioned in the type narrowing section of the documentation)
  • NASA's Power of 10: Rules for Developing Safety-Critical Code
    • One of those rules is "Use a minimum of two runtime assertions per function". This is one of the very few examples I've found supporting runtime assertions, and it's a really good one!
    • If it's good enough to keep resource-constrained software running for decades despite the harsh conditions of space, runtime assertions are probably good enough for the average application
  • Zig's runtime assertion system
    • Yea I know, this repository is TypeScript. But this was one of the very few production examples of a runtime assertion system. Everyone else seems to use assertions for testing only which seems like a missed opportunity. Hence the recommendation to use this library for runtime assertions.