Note from 2021: Just use TypeScript lol
888 d8b
888 Y8P
888
888888 888 888 88888b. .d88b. .d8888b 8888 .d8888b
888 888 888 888 "88b d8P Y8b 88K "888 88K
888 888 888 888 888 88888888 "Y8888b. 888 "Y8888b.
Y88b. Y88b 888 888 d88P Y8b. X88 d8b 888 X88
"Y888 "Y88888 88888P" "Y8888 "88888P' Y8P 888 "88888P'
888 888 888
Y8b d88P 888 d88P
"Y88P" 888 888P"
npm install typesjs # Node
bower install typesjs # Browser
var t = require('typesjs');
t("String", String);
t(23, Number);
t([], Array);
t([], String); // Throws a TypeError.
If you need a variable to be a String
, for example, typesjs can halt your code safely when it's not.
t.check('Lorem', String); // Returns true.
t.check(1999, String); // Returns false.
check
will return a boolean
based on the match.
try {
t([], String);
} catch(e) {
// ...
}
try/catch
blocks are your friend here.
function Message(text) {
this.text = text;
}
var message = new Message('Lorem');
t(message, Message);
Custom types are fully supported.
t(undefined, String); // Throws a TypeError.
t(undefined, String, false); // Returns true.
Just pass false
or "optional"
as the third parameter. This will allow for null
and undefined
values.
t(undefined, String, "optional"); // Returns true.
Passing "optional"
is more verbose, but perhaps more readable.
t("23", [Number, String]);
Just pass an array of types.
t.enabled = false;
When disabled, typesjs will always return true
.
Note: This changes the global setting for all the code using TypesJS. Only use this in applications and never in libraries.
Feel free to make a GitHub issue, or pull request. Thanks.