The `type` package offers a suite of runtime validation and processing utilities for fundamental JavaScript types, engineered for environments supporting ECMAScript 3 and higher without implying any transpilation. It specializes in bulletproof input argument normalization and validation, making it highly suitable for validating public API endpoints. The library's current stable version is 2.7.3, released in May 2024, and it follows an irregular release cadence, focusing on maintenance improvements and incremental feature additions like `BigInt` or `Map`/`Set` validation. Its core differentiators include a deep respect for JavaScript's inherent language nature and quirks, offering restricted forms of type coercion that explicitly reject invalid input while intelligently normalizing permissible type deviations. It provides `coerce`, `is`, and `ensure` utilities for various types, enabling developers to confirm types, safely coerce values, or strictly validate them with configurable error handling, including options for optional values and default fallbacks. It's explicitly positioned for basic type checks, recommending more powerful schema-based utilities like AJV or Joi for complex, deeply nested object structures.
npm install typeVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to normalize and validate function arguments using various `type` utilities, including string, date, natural number, and object checks, applying options like error messages, optionality, and default values for robust input handling.
For complex object validation, integrate a schema validation library like AJV or Hapi/Joi instead of trying to compose complex validations with `type` utilities.
Ensure your target environment natively supports the JavaScript features you are using, or implement your own transpilation/polyfill solution if needed. Do not rely on this library for environment compatibility.
Wrap calls to `*/ensure` in `try...catch` blocks to gracefully handle validation failures, or configure the `isOptional: true` option to accept `null`/`undefined` and `default` option to provide a fallback value.
When using `*/coerce`, always check for `null` in the returned value to determine if coercion was successful. Do not assume a non-null return value without checking.
Provide a value that satisfies the type constraints of the `*/ensure` utility, or configure the `isOptional: true` option to allow `null`/`undefined`, or set a `default` value.
Ensure your project is configured for CommonJS (e.g., by omitting `"type": "module"` in `package.json` or by using `.cjs` file extensions for CommonJS files), or use a build tool like Webpack or Rollup to bundle CommonJS modules for an ESM target.
No dependency data recorded yet.