"Si vis pacem, para bellum" - (Vegetius 5th century)
tcomb is a library for Node.js and the browser which allows you to check the types of JavaScript values at runtime with a simple and concise syntax. It's great for Domain Driven Design and for adding safety to your internal code.
Setup
npm install tcomb --save
Code example
A type-checked function:
; { t; t; return a + b;} ; // throws '[tcomb] Invalid value "s" supplied to Number'
A user defined type:
const Integer = t;
A type-checked class:
const Person = t; // methods are defined as usualPersonprototype { return ` `;}; const person = ; // throws '[tcomb] Invalid value undefined supplied to Person/name: String'
Docs
- API
- A little guide to runtime type checking and runtime type introspection (Work in progress)
Features
Lightweight
3KB gzipped, no dependencies.
Type safety
All models defined with tcomb
are type-checked.
Note. Instances are not boxed, this means that tcomb
works great with lodash, Ramda, etc. And you can of course use them as props to React components.
Based on set theory
- Blog post: JavaScript, Types and Sets - Part I
- Blog post: JavaScript, Types and Sets - Part II
Domain Driven Design
Write complex domain models in a breeze and with a small code footprint. Supported types / combinators:
- user defined types
- structs
- lists
- enums
- refinements
- unions
- intersections
- the option type
- tuples
- dictionaries
- functions
- recursive and mutually recursive types
Immutability and immutability helpers
Instances are immutable using Object.freeze
. This means you can use standard JavaScript objects and arrays. You don't have to change how you normally code. You can update an immutable instance with the provided update(instance, spec)
function:
const person2 = Person;
where spec
is an object containing commands. The following commands are compatible with the Facebook Immutability Helpers:
$push
$unshift
$splice
$set
$apply
$merge
See Updating immutable instances for details.
Speed
Object.freeze
calls and asserts are executed only in development and stripped out in production (using process.env.NODE_ENV !== 'production'
tests).
Runtime type introspection
All models are inspectable at runtime. You can read and reuse the informations stored in your types (in the meta
static member). See The meta object in the docs for details.
Libraries exploiting tcomb's RTI:
Easy JSON serialization / deseralization
Encodes / decodes your domain models to / from JSON for free.
- Blog post: JSON Deserialization Into An Object Model
Debugging with Chrome DevTools
You can customize the behavior when an assert fails leveraging the power of Chrome DevTools.
// use the default...t { throw '[tcomb] ' + message; // set "Pause on exceptions" on the "Sources" panel for a great DX}; // .. or define your own behaviort { console;};
Pattern matching
const result = t; console; // => 'a number'
Babel plugin
With babel-plugin-tcomb you can use type annotations:
: t.Number { return a + b;}
TypeScript definition file
Contributors
- Giulio Canti maintainer
- Becky Conning
func
combinator ideas and documentation - Andrea Lattuada
declare
combinator
Similar projects
License
The MIT License (MIT)