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

assert-or-return

v1.0.1

Published

Type-safe assertion library with decorator-based early returns for TypeScript

Downloads

1

Readme

assert-or-return

Type-safe assertion library with decorator-based early returns for TypeScript.

Installation

npm install assert-or-return

Features

  • 🎯 Type-safe assertions with TypeScript support
  • 🔄 Early returns via decorators (no wrapper functions needed)
  • 📦 Result type for explicit error handling
  • Zero dependencies
  • 🛡️ Full TypeScript support with declaration files

Usage

Basic Example

import { assert, HasAssertions, AssertResult, Ok, Err } from 'assert-or-return';

class Calculator {
  // With assert-or-return decorator (simplified)
  @HasAssertions()
  static add(a?: number, b?: number): AssertResult<number> {
    assert(a, "Parameter 'a' is required");
    assert(b, "Parameter 'b' is required");
    return Ok(a + b);
  }

  // Without decorator (manual error handling)
  static addWithoutAssertions(a?: number, b?: number): AssertResult<number> {
    if (!a) {
      return Err("Parameter 'a' is required");
    }
    if (!b) {
      return Err("Parameter 'b' is required");
    }
    return Ok(a + b);
  }
}

// Usage
const result1 = Calculator.add(1, 2);
if (result1.success) {
  console.log(result1.data); // 3
} else {
  console.log(result1.error); // Type-safe error handling
}

const result2 = Calculator.add(1); // Missing second parameter
console.log(result2); // { success: false, error: "Parameter 'b' is required" }

The decorator approach eliminates the need for manual if checks and return Err() statements, making your code more concise and readable.

How It Works

  1. assert(value, errorMessage) - Throws an assertion error if value is falsy
  2. @HasAssertions() - Decorator that catches assertion errors and returns them as AssertResult
  3. AssertResult<T> - Type-safe result type: { success: true, data: T } or { success: false, error: string }
  4. Ok(value) - Helper to create success results

Type Safety

The library provides full TypeScript support:

// TypeScript knows the types after assertions
function processUser(user?: User): AssertResult<string> {
  assert(user, "User is required");
  assert(user.name, "User name is required");
  
  // TypeScript knows user and user.name are non-null here
  return Ok(`Hello ${user.name}!`);
}

Requirements

  • Node.js: >=16.0.0
  • TypeScript: For full type safety (optional for JavaScript usage)
  • Decorators: Your tsconfig.json must have "experimentalDecorators": true

TypeScript Configuration

Add these options to your tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

API Reference

assert<T>(value: T, errorMessage: string): asserts value is NonNullable<T>

Asserts that a value is truthy. If the assertion fails, throws an error that gets caught by the @HasAssertions() decorator.

@HasAssertions()

Class method decorator that catches assertion errors and converts them to AssertResult return values.

AssertResult<T>

Type representing either success or failure:

  • { success: true, data: T } - Successful result with data
  • { success: false, error: string } - Failed result with error message

Ok<T>(data: T): AssertResult<T>

Helper function to create successful results.

Err(error: string): AssertResult<never>

Helper function to create error results.

Limitations

Decorator Scope

The @HasAssertions() decorator only works on class methods. It cannot be used on:

  • Standalone functions
  • Arrow functions
  • Object methods (outside of classes)
// ✅ Works - class method
class MyClass {
  @HasAssertions()
  static myMethod(): AssertResult<string> {
    assert(someValue, "Error message");
    return Ok("success");
  }
}

// ❌ Doesn't work - standalone function
@HasAssertions() // This won't work
function myFunction(): AssertResult<string> {
  assert(someValue, "Error message");
  return Ok("success");
}

Behavior Without Decorator

When assert() is used without the @HasAssertions() decorator, it behaves like Node.js's built-in assert module - it simply throws an AssertionError that you must catch yourself:

import { assert } from 'assert-or-return';

function withoutDecorator(value?: string) {
  try {
    assert(value, "Value is required"); // Throws AssertionError if value is falsy
    console.log("Value is:", value);
  } catch (error) {
    console.log("Caught error:", error.message); // Manual error handling required
  }
}

This behavior is consistent with Node.js's node:assert module, making the library familiar to developers already using assertions.

License

MIT