Practical run-time type checking using adt.js.
adt.typecheck.js syntax is designed for CoffeeScript.
It is certainly possible to use the library with JavaScript proper, however as
an EDSL
adt.typecheck
is easiest to read using CoffeeScript shorthands. For this
reason all of the examples here use CoffeeScript notation.
Two words of warning:
- Debugging wrapped up function calls can be painful. When debugging it is occasionally helpful to comment out the signature to avoid stepping through lots of this typechecking code.
- adt-typecheck.js is designed for flexibility rather than performance, especially since ADT's aren't natively implemented in JavaScript. If JavaScript performance is an issue for you, you may want to reconsider using a run-time typechecker.
Also, please note that adt-typecheck.js does not have an official release yet. Parts of the API can and will change over time.
# TODO
# TODO
# TODO
Note that adt-typecheck.js will add some overhead to all of your functions signed in this way and is not appropriate for situations where high performance is required. This style of typechecking is probably more appropriate for server-side code than browser-side code (especially in situations where old and inefficient browsers need to be supported).
A proxy can be used in place of adt.typecheck.signature.function
in production
mode if performance is a concern. E.g.
if isDevelopmentMode
fn = adt.typecheck.signature.function
else
fn = (schema, f) -> f
# TODO
The same proxy as the one for adt.typecheck.signature.function
can be used in
place of adt.typecheck.signature.chainFunction
.
if isDevelopmentMode
cfn = adt.typecheck.signature.chainFunction
else
cfn = (schema, f) -> f
The following snippet of code can be used to create a middleware for express that typechecks your incoming request body.
# TODO
Use it like this:
# TODO