A lightweight TypeScript utility for runtime checks using invariant patterns
npm install ant-invariant- 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.
---
ant-invariant via npm or yarn:bash
Using npm
npm install ant-invariantUsing yarn
yarn add ant-invariant
`Example
`typescript
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'");
}
``