Coder Social home page Coder Social logo

babel-plugin-tcomb's Introduction

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.

babel-plugin-tcomb is a babel plugin for runtime type checking using tcomb. You can annotate a function (arguments and return type) with tcomb types.

How it works

import t from 'tcomb';

// add type annotations
function foo(x: t.Number, y: t.String): t.String {
  return x + y;
}

// compiled to
function foo(x: t.Number, y: t.String): t.String {

  // check the arguments: tcomb types are identity functions
  // containing type asserts
  x = t.Number(x);
  y = t.String(y);

  // exec the original function
  const ret = function (x, y) {
    return x + y;
  }(x, y);

  // check the return type
  return t.String(ret);
}

Now the foo function is type-checked, this means...

foo(1, 'a'); // => ok
foo(1, 2); // => ...will throws "[tcomb] Invalid value 2 supplied to String"

Caveats

  • Destructuring and typed default values are not (yet) supported

Setup

First, install via npm.

npm install --save-dev babel-plugin-tcomb

Then, in your babel configuration (usually in your .babelrc file), add "tcomb" to your list of plugins:

{
  "plugins": ["tcomb"]
}

or

"env": {
  "development": {
    "plugins": [
      "tcomb"
    ]
  }
}

Webpack config

module: {
  loaders: [
    {
      test: /\.jsx?$/,
      loader: 'babel?plugins=babel-plugin-tcomb'
    }
  ]
}

Features

struct combinator

const Person = t.struct({
  name: t.String
});

function foo(person: Person) {
  return person.name;
}

// compiles to
function foo(person: Person) {
  person = Person(person);

  return person.name;
}

refinement combinator

const Integer = t.refinement(t.Number, (n) => n % 1 === 0);

function foo(x: Integer) {
  return x;
}

// compiles to
function foo(x: Integer) {
  x = Integer(x);

  return x;
}

maybe combinator

function foo(x: ?t.String) {
  return x;
}

// compiles to
function foo(x: ?t.String) {
  x = t.maybe(t.String)(x);

  return x;
}

list combinator

function foo(x: Array<t.String>) {
  return x;
}

// compiles to
function foo(x: Array<t.String>) {
  x = t.list(t.String)(x);

  return x;
}

tuple combinator

function foo(x: [t.String, t.Number]) {
  return x;
}

// compiles to
function foo(x: [t.String, t.Number]) {
  x = t.tuple([t.String, t.Number])(x);

  return x;
}

union combinator

function foo(x: t.String | t.Number) {
  return x;
}

// compiles to
function foo(x: t.String | t.Number) {
  x = t.union([t.String, t.Number])(x);

  return x;
}

dict combinator

function foo(x: {[key: t.String]: t.Number}) {
  return x;
}

// compiles to
function foo(x: { [key: t.String]: t.Number }) {
  x = t.dict(t.String, t.Number)(x);

  return x;
}

intersection combinator

function foo(x: t.Number & t.String) {
  return x;
}

// compiles to
function foo(x: t.Number & t.String) {
  x = t.intersection([t.Number, t.String])(x);

  return x;
}

Arrow functions

const f = (x: t.String) => x;

// compiles to
const f = x => {
  x = t.String(x);
  return x;
};

Classes

class A {
  foo(x: t.String): t.String {
    return x;
  }
}

// compiles to
class A {
  foo(x: t.String): t.String {
    x = t.String(x);

    const ret = function (x) {
      return x;
    }(x);

    return t.String(ret);
  }
}

babel-plugin-tcomb's People

Contributors

gcanti avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.