Type-Safe Assertion Library for TypeScript
typed-assert is a lightweight, zero-dependency assertion library designed specifically for TypeScript 3.7+ environments. It leverages TypeScript's assertion functions feature to perform runtime checks that simultaneously narrow types at compile-time, promoting the use of `unknown` for untrusted input instead of `any`. The library provides a suite of common assertion functions (`isString`, `isNumber`, `isRecord`, etc.) and combinators (`isArrayOf`, `isOneOf`, `isOption`), allowing developers to build robust type guards and custom composite assertions. As of version 1.0.9, it's a stable release with no explicit major release cadence, but it's actively maintained. A key differentiator is its compile-time type narrowing alongside runtime validation, contrasting with traditional assertion libraries like Chai or Jest's `expect` which primarily offer runtime validation without type inference benefits. For more complex schema validation needs, the documentation explicitly recommends considering `zod` as an alternative due to `typed-assert`'s simpler scope.
Common errors
-
TS2571: Object is of type 'unknown'.
cause Attempting to access properties on an `unknown` type without prior assertion or type narrowing.fixApply an appropriate `typed-assert` function (e.g., `t.isRecord`, `t.isString`, `t.isArrayOf`) before accessing properties on an `unknown` variable to narrow its type.
Warnings
- breaking This library relies on TypeScript's 'assertion functions' feature, introduced in TypeScript 3.7. Projects using older TypeScript versions will not be able to utilize this package effectively and will encounter compilation errors related to unsupported syntax.
- gotcha `typed-assert` is designed for focused type-safe assertions and type narrowing. For highly complex data validation schemas, especially those involving recursive types, transformations, or extensive error reporting, it explicitly recommends considering a more comprehensive validation library like Zod.
Install
-
npm install typed-assert -
yarn add typed-assert -
pnpm add typed-assert
Imports
- t
const t = require('typed-assert');import * as t from 'typed-assert';
- isString
import * as t from 'typed-assert'; const { isString } = t;import { isString } from 'typed-assert'; - safeJsonParse
import * as t from 'typed-assert'; const myParse = t.safeJsonParse;
import { safeJsonParse } from 'typed-assert';
Quickstart
import * as t from 'typed-assert';
interface Config {
readonly a: {
readonly b: 'c';
readonly d: string;
};
readonly f?: number;
}
// Custom assertion function for a complex type
function assertConfig(input: unknown): asserts input is Config {
t.isRecordWithKeys(input, ['a']); // 'f' is optional, so not required here
t.isRecordWithKeys(input.a, ['b', 'd']);
t.isExactly(input.a.b, 'c');
t.isString(input.a.d);
t.isOption(input.f, t.isNumber); // Handles optional numeric property
}
// Example usage with untrusted input
const untrustedInput1: unknown = {
a: { b: 'c', d: 'hello' },
f: 123
};
assertConfig(untrustedInput1);
// At this point, untrustedInput1 is narrowed to Config
console.log(untrustedInput1.a.d); // 'hello'
const untrustedInput2: unknown = {
a: { b: 'c', d: 456 } // 'd' should be string
};
try {
assertConfig(untrustedInput2);
} catch (error: any) {
console.error(`Assertion failed: ${error.message}`);
// Expected: 'Value must be a string: 456'
}