@aliaksandarpratashchyk/suspenders
v1.1.0
Published
TypeScript assertion and type-guard toolkit for runtime validation and type narrowing.
Downloads
26
Maintainers
Readme
Suspenders
Suspenders is a small TypeScript-first assertion and type-guard toolkit for runtime validation and type narrowing.
Installation
npm i @aliaksandarpratashchyk/suspendersUsage
import { assert, isNumber, safe, unsafe } from "@aliaksandarpratashchyk/suspenders";
const value: unknown = "123";
// Type guard: narrow in an if-statement
if (isNumber(value)) {
value.toFixed(2);
}
// Assertion: throws and narrows the type (via `asserts`)
assert(typeof value === "string");
assert.string(value);
// Safe cast: returns a typed value or throws a TypeError
const asNumber = safe.number;
const n = asNumber(123);
// Unsafe cast: no runtime checks
const definitelyNumber = unsafe<number>(value);API Reference
- Run
npm run buildto regenerate API docs intodocs/.
Scripts
npm test— run the Jest suite, regenerateCOVERAGE.md, and updatecoverage.svg.npm run build— bundle the library and regenerate API docs (docs/).npm run lint/npm run format— lint and format the codebase.
Publishing
GitHub Actions runs tests on pushes/PRs and publishes to npm when the version in package.json changes on main. Set NPM_TOKEN in repository secrets to enable publishing.
