@philiprehberger/invariant-ts
v0.1.3
Published
Tiny runtime assertion with TypeScript narrowing
Readme
@philiprehberger/ts-invariant
Tiny runtime assertion with TypeScript narrowing.
Installation
npm install @philiprehberger/ts-invariantUsage
import { invariant, assertNever, unreachable } from '@philiprehberger/ts-invariant';
const user = getUser(id);
invariant(user, 'User %s not found', id);
user.name; // narrowed to User
// Lazy message
invariant(data, () => `Expected data, got ${typeof data}`);
// Exhaustiveness checking
type Shape = 'circle' | 'square';
switch (shape) {
case 'circle': return handleCircle();
case 'square': return handleSquare();
default: assertNever(shape);
}Custom Error Classes
import { invariantAs } from '@philiprehberger/ts-invariant';
class NotFoundError extends Error {
constructor(message?: string) {
super(message);
this.name = 'NotFoundError';
}
}
invariantAs(NotFoundError, user, 'User not found');API
| Function | Description |
|----------|-------------|
| invariant(condition, message?, ...args) | Assert condition is truthy, narrows type |
| invariantAs(ErrorClass, condition, message?) | Assert with custom error type |
| assertNever(value) | Exhaustiveness checking for switch/if chains |
| unreachable(message?) | Mark unreachable code paths |
| InvariantError | Error class thrown by invariant() |
Development
npm install
npm run build
npm testLicense
MIT
