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"
Installation
npm install typesjs # Node bower install typesjs # Browser
What can TypesJS do for me?
var t = ; ;;; ; // Throws a TypeError.
If you need a variable to be a String
, for example, typesjs can halt your code safely when it's not.
What if I dislike errors?
t; // Returns true.t; // Returns false.
check
will return a boolean
based on the match.
What if I do like errors?
try ; catche // ...
try/catch
blocks are your friend here.
What if I make my own types?
{ thistext = text;}var message = 'Lorem';;
Custom types are fully supported.
What if a value is null or undefined?
; // Throws a TypeError.
What if a value isn't required though?
; // Returns true.
Just pass false
or "optional"
as the third parameter. This will allow for null
and undefined
values.
; // Returns true.
Passing "optional"
is more verbose, but perhaps more readable.
What if I want to allow multiple types?
;
Just pass an array of types.
What if I want to disable typesjs, during runtime?
tenabled = 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.