npm install tcomb-atsAn assert library for AtScript based on tcomb type combinators.
```
npm install tcomb-ats
Add the following keys to your traceur options:
`js`
{
"types": true,
"typeAssertions": true,
"typeAssertionModule": "path/to/tcomb-ats",
"annotations": true // if you also want annotations
}
Set "typeAssertions": false in production.
``
git clone https://github.com/gcanti/tcomb-ats.git
cd tcomb-ats
npm install
gulp
Open ./example/index.html in a browser.
`js
var t = require('tcomb-ats');
var Str = t.Str;
// strings
var s: Str = 'a'; // ok
var s: Str = null; // throws (tcomb's primitives are not nullable)
var s: Str = 1; // throws
// numbers
var Num = t.Num;
var n: Num = 1; // ok
// booleans
var Bool = t.Bool;
var b: Bool = true; // ok
// you can use other irreducible types provided by tcomb
// or define your own, see tcomb documentation
`
Use the maybe combinator:
`js`
var s: maybe
var s: maybe
`js
function sum(a: Num, b: Num): Num {
return a + b;
}
sum(1, 2); // ok
sum(1, 'a'); // throws
`
Use the list combinator:
`js
var list = t.list;
var a: list
var b: list
// you can also use the Array syntax`
var c: Array
`js
class Person {
constructor(name: Str, surname: Str) {
this.name = name;
this.surname = surname;
}
}
var p1: Person = new Person('Giulio', 'Canti'); // ok
var p2: Person = new Person('Giulio'); // throws
`
Use the tuple combinator:
`js
var tuple = t.tuple;
var t: tuple
var t: tuple
`
Use the dict combinator:
`js
var dict = t.dict;
var d: dict
var d: dict
`
Use the enums combinator:
`js
var Align = t.enums.of('left center right');
var e: Align = 'left'; // ok
var e: Align = 'justify'; // throws
`
Use the subtype combinator:
`js
var Positive = t.subtype(Num, function (n) {
return n >= 0;
});
var n: Positive = 1; // ok
var n: Positive = -1; // throws
var n: Positive = 'a'; // throws
`
Use the union combinator:
`js
var StrOrNum = t.union([Str, Num]);
var u: StrOrNum = 'a'; // ok
var u: StrOrNum = 1; // ok
var u: StrOrNum = true; // throws
`
`js
var Person = t.struct({
name: t.Str,
surname: t.Str
});
var p: Person = new Person({
name: 'Giulio',
surname: 'Canti'}
); // ok
var p: Person = 1; // throws
``
The MIT License (MIT)