Every developer knows that JavaScript is not a static typed language, a useful feature eliminating lots of bugs a language like Typescript has been created as a remedy to that important lack of safety . Even for code modules counting less than a few tens of lines, it’s easy to forget what exactly is the type of a variable declared at the begining of the file and then make a mistake when assining a wrong type to that variable.
TypexJS is a simple convention designed to avoid such mistakes: by only adding a mnemonic letter at the end of each identifier to specify the variable type of course, this scheme also applies to constants! .
This simple adjonction has two main benefices:
Just an example, the JavaScript String.prototype.split
method returns an array of Strings:
var str = 'The quick brown fox jumps over the lazy dog.';
var words = str.split(' ');
Two different words for two tightly related entities! Isn’t it semantically more meaningful to use the same identifier with different specifiers?
var sentence_s = 'The quick brown fox jumps over the lazy dog.';
var sentence_a = sentence_s.split(' ');
A more tricky example with smart inline type coercion tricks! :
let number_s = '123'
let number_n = +number_s //: cast to Number
number_s = '' + ++number_n //: cast to String
The minimalist and easy to memorize convention defined by TypexJS can help you to write a cleaner and more meaninful code while shortening its documentation. Follow on with specifiers definitions.