tcomb is a JavaScript library for runtime type checking and Domain-Driven Design (DDD), suitable for both Node.js and browser environments. It provides a concise syntax for defining and validating data structures, enhancing code safety during development. The current stable version is 3.2.29, with a recent cadence focused on bug fixes and TypeScript definition improvements. A key differentiator is its lightweight nature (3KB gzipped, no dependencies) and its foundation in set theory for type definition. It supports various type combinators (structs, lists, enums, refinements, unions), immutability helpers compatible with Facebook's Immutability Helpers, and runtime type introspection. Crucially, tcomb is designed to be *disabled in production* (its checks are stripped out for performance), with `io-ts` or `tcomb-validation` recommended for production-grade type validation.
npm install tcombVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define type-checked functions, create custom refined types, build structured objects (structs) with runtime validation, and perform immutable updates on instances using tcomb's utilities.
For production-grade runtime type validation, consider using alternative libraries from the same author, such as `io-ts` or `tcomb-validation`. Do not deploy tcomb-dependent code to production expecting runtime checks to be active.
Ensure you understand the distinction between compile-time TypeScript validation and tcomb's runtime validation. For automatic runtime checks based on type annotations, consider integrating `babel-plugin-tcomb` into your build process.
Use the `t.update(instance, spec)` utility with immutability helpers (`$set`, `$merge`, `$push`, etc.) to create new instances with desired modifications, rather than attempting to mutate the original instance.
Ensure the value passed to the tcomb type check (e.g., `t.Number(value)`) matches the expected type. In TypeScript, this might indicate a mismatch between static and runtime types.
Provide a valid value for all required fields in the struct's constructor. For fields that can be `null` or `undefined`, define them using `t.maybe(Type)`.
No dependency data recorded yet.