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.
npm install typed-assertVerified import paths — ran on the pinned version, not inferred.
Demonstrates defining a custom type assertion for a complex interface and its application to untrusted `unknown` input, showcasing compile-time type narrowing.
Upgrade your project's TypeScript version to 3.7 or newer. Ensure your `tsconfig.json` targets an appropriate ES version.
Evaluate the complexity of your data validation needs. If simple type narrowing is sufficient, `typed-assert` is appropriate. For intricate schemas, investigate libraries like Zod, Yup, or Joi.
Apply an appropriate `typed-assert` function (e.g., `t.isRecord`, `t.isString`, `t.isArrayOf`) before accessing properties on an `unknown` variable to narrow its type.
No dependency data recorded yet.