Deftypes.js
JavaScript type annotaion DSL library for structure and function.
Provide simple DSL and No side effects
It is type checker on execution(not static).
Deftypes can avoid all side effects if you want. It may be also good on testing framework.
Install
Examples are coffee-script.
Node
$ npm install deftypes
require 'deftypes'
Browser
$ bower install deftypes
<script src="bower_components/deftypes/deftypes.js"></script>
or download deftypes.js of this directory
<script src="master/deftypes.js"></script>
Deftypes()
It provides DSLs => def, defun, T
How to use
Struct Definition
Point = x: Numbery: Numberp1 = def Pointx:1y:2 #=> {x: 1, y:2} p2 = def Pointx:1z:2 #=> type error
Typed Array
list = def TInt123
(T.Int and T.Fload are defined by default)
Nullable
NullableNumber = n: TNullableNumberp1 = def NullableNumbern:1p2 = def NullableNumbern:null
Typed Hash
id_table = def THashStringNumber A:1 B:2 C:3
Key accepts only Number or String (but it doesn't check yet)
Function Definition
check arguments and returned object
f1 = def TFuncNumberNumberString ", "f112 #=> "1, 2" f1""2 #=> argument error
find_index = def TFuncNumberNumberTNullableNumber if index = arrindexOfn is -1 then null else index find_index3454 #=> 1 find_index3459 #=> null
Function DSL
f2 = defun NumberNumberString ", "
Any Type
events = def Objectrequire'events'list = def Tany0""null
Function Scope with type check
p = def Pointx:1y:2def Pointp-> @x = 3 # Type Error def Pointp-> @y = "not number"
Transparent mode
if option.transparent is true, typechecker does nothing, passing through def like transparent for avoiding performance down.
require 'deftypes'option.transparent = true
Deftypes.option.transparent = true
TODO
- trait feature
- struct inheritance
- valid error message