1.0.2 • Published 8 months ago

ant-invariant v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

ant-invariant

A lightweight TypeScript utility for runtime condition checks using invariant patterns. Ant-Invariant ensures your conditions are met at runtime and throws meaningful errors otherwise.

Features

  • TypeScript support: Utilizes asserts for type narrowing.
  • Lightweight: Simple and minimal with no external dependencies.
  • Runtime checks: Ensure that required conditions are met during runtime.

Install

Install ant-invariant via npm or yarn:

# Using npm
npm install ant-invariant

# Using yarn
yarn add ant-invariant

Example

import { invariant } from 'ant-invariant';

interface Product {
    id: number;
    name: string;
    price: number;
}

function validateProduct(product: unknown): asserts product is Product {
    invariant(
        typeof product === "object" && product !== null,
        "Product must be a non-null object"
    );
    invariant("id" in product && typeof product.id === "number", "Product must have a numeric 'id'");
    invariant("name" in product && typeof product.name === "string", "Product must have a string 'name'");
    invariant("price" in product && typeof product.price === "number", "Product must have a numeric 'price'");
}
1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago