• Stars
    star
    886
  • Rank 51,520 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Static and runtime type checking for JavaScript in the form of a Babel plugin.

Babel Typecheck

This is a Babel plugin for static and runtime type checking using flow type annotations.

Build Status

Deprecated

This project is now deprecated, we're now focussing on flow-runtime.


Note: Now requires babel 6.1, babel 5 users see the 2.x branch.

What?

Turns code like this:

function sendMessage (to: User, message: string): boolean {
  return socket.send(to, message);
}

into code like this:

function sendMessage(to, message) {
  var _socket$send;

  if (!(to instanceof User)) throw new TypeError("Value of argument 'to' violates contract.");
  if (typeof message !== "string") throw new TypeError("Value of argument 'message' violates contract.");
  _socket$send = socket.send(to, message);
  if (typeof _socket$send !== "boolean") throw new TypeError("Function 'sendMessage' return value violates contract.");
  return _socket$send;
}

And guards against some silly mistakes, for example the following code will fail to compile with a SyntaxError, because the function can return the wrong type.

function foo (): boolean {
  if (Math.random() > 0.5) {
    return "yes"; // <-- SyntaxError - string is not boolean
  }
  else {
    return false;
  }
}

function bar (input: string = 123): string { // <-- SyntaxError: default value is not string
  return input + "456";
}

Installation

First, install via npm.

npm install --save-dev babel-plugin-typecheck

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

{
  "plugins": [
    ["typecheck", {
      "disable": {
        "production": true
      }
    }]
  ]
}

The example configuration will disable typecheck when NODE_ENV=production which is usually preferable for performance reasons.

Important: This plugin has a dependency on babel-plugin-syntax-flow and babel-plugin-transform-flow-strip-types. Without syntax-flow, babel will be unable to parse the flow annotation syntax. Without transform-flow-strip-types, the type annotations will be included in the output which will make it unparsable by JS engines.

If you are not already using the babel-preset-react plugin, you must install those plugins and include them in your babel configuration (usually .babelrc). Put them after typecheck in the list, e.g.

{
  "plugins": ["typecheck", "syntax-flow", "transform-flow-strip-types"]
}

If you are using babel-preset-react you can ignore this warning.

Note Depending on your babel configuration you may encounter issues where typecheck interferes with other transformations. This can almost always be fixed by adjusting your preset order and setting "passPerPreset": true in your .babelrc.

Examples

The basic format is similar to Flow Type Annotations.

Here are a few examples of annotations this plugin supports:

function foo(
    aNum: number,
    anOptionalString: ?string, // will allow null/undefined
    anObject: Object,
    aDate: Date,
    anError: Error,
    aUnionType: Object|string,
    aClass: User,
    aShape: {foo: number, bar: ?string},
    anArray: Array,
    arrayOf: string[] | Array<string>,
    {x, y}: {x: string, y: number}, // destructuring works
    es6Defaults: number = 42
) : number {
  return aNum;
}

Importing and Exporting types.

You can reuse types across modules using an extension of the ES6 module syntax:

places.js:

export type CsvDataType = Array<Array<String>>;
export type LocationType = {
    country: string,
    sourceNid: string,
    locationNid: string,
    name: string,
    url: string,
    alternativeUrl: ?string,
    street1: ?string
};

widget.js:

import type {
    CsvDataType,
    LocationType
} from './places';

// You can now use CsvDataType and LocationType just like any other type.

Note that in contrast to flow, an imported type must be an actual type and cannot be a class or other concrete value.

Optimization

In cases where typecheck can statically verify that the return value is of the correct type, no type checks will be inserted, for instance:

function bar (): string|Object {
  if (Math.random() > 0.5) {
    return "yes";
  }
  else {
    return {
      message: "no"
    };
  }
}

will produce no type checks at all, because we can trivially tell that the function can only return one of the two permitted types. This is also true for simple cases like:

function createUser (): User {
  return new User(); // <-- no typecheck required
}

This is currently quite limited though, as the plugin can only statically infer the types of literals and very simple expressions, it can't (yet) statically verify e.g. the result of a function call. In those cases a runtime type check is required:

function createUser (): User {
  return User.create(); // <-- produces runtime typecheck
}

Changes in 3.5.0

Supports various number types:

  • int8
  • uint8
  • int16
  • uint16
  • int32
  • uint32
  • float32
  • float16

Example:

function demo (input: uint8): uint16 {
  return input * input;
}

demo(1); // ok
demo(128); // ok
demo(255); // ok
demo(-1); // TypeError
demo(12.34); // TypeError
demo(1024); // TypeError
demo('nope'); // TypeError

Changes in 3.0.0

Supports type aliases:

type Foo = string|number;

function demo (input: Foo): string {
  return input + '  world';
}

demo('hello'); // ok
demo(123); // ok
demo(["not", "a", "Foo"]); // fails

Better static type inference

function demo (input: string): string[] {
  return makeArray(input); // no return type check required, knows that makeArray is compatible
}

function makeArray (input: string): string[] {
  return [input];
}

Type propagation

function demo (input: string): User {
  const user = new User({name: input});
  return user; // No check required, knows that user is the correct type
}

Assignment tracking

let name: string = "bob";

name = "Bob"; // ok
name = makeString(); // ok
name = 123; // SyntaxError, expected string not number

function makeString (): string {
  return "Sally";
}

Type casting

let name: string = "bob";

name = "Bob";
((name: number) = 123);
name = 456;
name = "fish"; // SyntaxError, expected number;

Array type parameters

function demo (input: string[]): number {
  return input.length;
}

demo(["a", "b", "c"]); // ok
demo([1, 2, 3]); // TypeError

Shape tracking

type User = {
  name: string;
  email: string;
};

function demo (input: User): string {
  return input.name;
}

demo({}); // TypeError
demo({name: 123, email: "[email protected]"}); // TypeError
demo({name: "test", email: "[email protected]"}); // ok

Pragmas

Sometimes you might need to disable type checking for a particular file or section of code. To ignore an entire file, add a comment at the top level scope of the file:

// typecheck: ignore file
export function wrong (input: string = 123): boolean {
  return input + ' nope';
}

To ignore a particular statement:

let foo: string = "hello world";
// typecheck: ignore statement
foo = 123;

Note: Because of how typecheck works, it's not possible to ignore individual lines, only entire statements or files. So if you ignore e.g. an if statement, the entire body of that statement will be ignored.

You can also control the disabling and enabling of type checking using the plugin options and the @typecheck pragma. Type checking will be enabled only for files where any of the configured only values are found in the @typecheck pragma. With babel configuration:

"plugins": [
  ["typecheck", { only: ["production", "test"] }],
  ...
  ]

This file would have typechecks enabled

// @typecheck: production, some

Whereas this file would not:

// @typecheck: any, some

License

Published by codemix under a permissive MIT License, see LICENSE.md.

More Repositories

1

fast.js

Faster user-land reimplementations for several common builtin native JavaScript functions.
JavaScript
3,412
star
2

ts-sql

A SQL database implemented purely in TypeScript type annotations.
TypeScript
3,181
star
3

deprank

Use PageRank to find the most important files in your codebase.
TypeScript
878
star
4

yii2-localeurls

Automatic locale/language management for URLs
PHP
412
star
5

babel-plugin-closure-elimination

A Babel plugin which eliminates closures from your JavaScript wherever possible.
JavaScript
369
star
6

babel-plugin-contracts

Design by Contract for JavaScript via a Babel plugin.
JavaScript
266
star
7

babel-plugin-macros

Hygienic, non-syntactic macros for JavaScript via a Babel plugin.
JavaScript
261
star
8

oriento

Former official node.js driver for OrientDB. Fast, lightweight, uses the binary protocol. Now deprecated.
JavaScript
196
star
9

htmling

Polymer / HTML5 templating syntax for node.js
JavaScript
177
star
10

yii2-dockerized

A template for docker based Yii 2 applications
PHP
169
star
11

yii2-excelexport

A utility to quickly create Excel files from query results or raw data
PHP
102
star
12

gitignore-parser

A simple .gitignore parser for node.js
JavaScript
97
star
13

contractual

Unobtrusive, backwards compatible, syntactic sugar for Design by contract in JavaScript.
JavaScript
72
star
14

babel-plugin-trace

This is a Babel plugin which adds a straightforward, declarative syntax for adding debug logging to JavaScript applications.
JavaScript
63
star
15

yii2-configloader

Build configuration arrays from config files and env vars.
PHP
61
star
16

yii2-streamlog

A Yii 2 log target for streams in URL format
PHP
52
star
17

yii2-dockerbase

Yii 2 base image for dockerized yii2 projects
Shell
39
star
18

YiiElasticSearch

Elastic Search client for Yii
PHP
33
star
19

malloc

Simple malloc() & free() implementation for node.js, built on top of array buffers.
JavaScript
25
star
20

reign

A persistent, typed objects implementation for node.js and the browser.
JavaScript
23
star
21

binary-protocol

Easy, fast, writers and readers for implementing custom binary protocols in node.js.
JavaScript
20
star
22

oauth2yii

An OAuth2 client / server extension for the Yii framework
PHP
17
star
23

modeling

Fast and flexible data models for node.js and the browser.
JavaScript
15
star
24

restyii

A RESTful extension for Yii.
PHP
15
star
25

babel-plugin-hyperhtml

Babel plugin which compiles JSX into hyperHTML
JavaScript
12
star
26

yii2-excel-message

Translate messages via Excel files
PHP
12
star
27

backing

Provides a virtual address space for large segments of memory via JavaScript ArrayBuffers, and operations for allocating and freeing within the address space, optionally via a simple reference counting garbage collector.
JavaScript
11
star
28

validating

Quick and easy validators for node.js and the browser.
JavaScript
10
star
29

babel-plugin-conditional

Conditionally applies a set of babel plugins based on the result of an expression evaluated at runtime.
JavaScript
10
star
30

yii2-bs3activeform

A Bootstrap 3 enhanced ActiveForm for Yii 2
PHP
9
star
31

url-route

Web component providing URL routing
JavaScript
9
star
32

htmling-demo-app

HTMLing demo running on express
CSS
8
star
33

garbage-collector

A garbage collector for JavaScript built on top of typed arrays.
JavaScript
8
star
34

geonames-importer

Imports geonames data into elasticsearch
JavaScript
7
star
35

orientdb-protobufs

An experiment to see how the orientdb binary protocol could look if it used protocol buffers.
Java
6
star
36

handlebarsphp

Transpiles handlebars templates into native PHP templates
PHP
6
star
37

atomicbuffers

Atomic `readInt32()`, `writeInt32()`, `readUInt32()` and `writeUInt32()` for node.js buffers.
JavaScript
6
star
38

classing

Fluent classes for node.js and the browser.
JavaScript
6
star
39

dispatching

Tiny routing / dispatch library for node and the browser.
JavaScript
5
star
40

casting

Tiny type casting library for node.js and the browser.
JavaScript
5
star
41

php-orientdb

A fast PHP driver for the OrientDB binary protocol.
PHP
5
star
42

obligations

Tiny JavaScript library for preconditions and postconditions, intended for use with Contractual.
JavaScript
4
star
43

AccessRestrictable

A Yii ActiveRecordBehavior that automatically applies conditions for access restriction to every query.
PHP
2
star
44

component-testing-library

A library for testing component driven UIs
TypeScript
2
star
45

bootstrap-css

Twitter Bootstrap CSS / LESS packaged for component.js instead of bower
CSS
2
star
46

miming

Processing and formatting for various mime types.
JavaScript
2
star
47

bs3activeform

A lightweight utility to render Bootstrap 3 forms in Yii
PHP
2
star
48

handlebarsgen

An extendable static code generator for handlebars templates, targetting languages other than JavaScript, e.g. PHP
CoffeeScript
2
star
49

malloc-append

Simple append-only alloc() implementation on top of buffers and array buffers.
JavaScript
1
star
50

jsx-email-nextjs

Reproduce an error with renderToStaticMarkup() in Next.js
TypeScript
1
star
51

bootstrap-tooltip

Twitter Bootstrap Tooltip plugin packaged for component.js instead of bower
JavaScript
1
star
52

bencha

Mocha-esque UI for the excellent benchmarkjs benchmarking library
CoffeeScript
1
star
53

bootstrap-transition

Twitter Bootstrap Transition plugin packaged for component.js instead of bower
JavaScript
1
star
54

urlrouter

Tiny URL routing for the browser
CoffeeScript
1
star
55

bootstrap-affix

Twitter Bootstrap Affix plugin packaged for component.js instead of bower
JavaScript
1
star
56

bootstrap-scrollspy

Twitter Bootstrap Scrollspy plugin packaged for component.js instead of bower
JavaScript
1
star