Typeforce is a JavaScript library designed for biased runtime type checking, offering a comprehensive suite of utilities to enforce data structures and primitive types. As of version 1.18.0, it provides a flexible API for defining complex type schemas, including support for arrays, recursive objects, optional properties (`?`), sum types (`anyOf`), and intersection types (`allOf`). A notable feature is its extensibility through custom type functions, allowing developers to define domain-specific validations. It differentiates itself by offering specialized modules for non-throwing error handling (`typeforce/nothrow`) and asynchronous validation (`typeforce/async`), catering to different error management strategies. While a specific release cadence isn't detailed, its versioning suggests ongoing maintenance and feature development, making it a robust choice for projects requiring strict data validation without relying on static type systems.
npm install typeforceVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic, recursive, custom, and strict type checking, along with the no-throw variant and performance tips like precompilation.
Avoid `quacksLike` if your codebase is minified with name mangling, or ensure your build configuration preserves `Function.name` for relevant classes. Consider using alternative structural checks if possible.
Do not depend on verbatim error message strings. Instead, check for the presence of an error or the general type of error (e.g., `TypeError`) if specific error handling is required. Use the `typeforce/nothrow` module for cleaner conditional logic based on validation success/failure.
Ensure your schema comprehensively defines all expected properties if strict mode is enabled. If properties are optional, mark them with `?Type` (e.g., `?String`) in the schema. For properties that can be anything, define them with `typeforce.any` or `typeforce.Object` as appropriate.
Ensure the type string or type object provided to `typeforce` accurately reflects the expected type of the `value` being validated. For example, `typeforce('Array', someArray)` instead of `typeforce('Number', someArray)`.If extra properties are allowed, remove the `true` argument for strictness. If they are not allowed, ensure your schema lists all permissible properties, using `?Type` for optional ones.
Check the order and types of elements in the array being validated against `typeforce.tuple()`. Each element must precisely match the corresponding type in the tuple definition.
This is often expected behavior for `typeforce/nothrow`. The fix involves checking the return value of the `typeforceNoThrow` call; if it's `false`, then `typeforceNoThrow.error.message` will contain the validation failure reason.
No dependency data recorded yet.