Sep 22, 2013 at 1:48 AM
Edited Sep 22, 2013 at 2:16 AM
There is a great feature in F# called units of measure (http://msdn.microsoft.com/en-us/library/dd233243.aspx
), when one can assign a unit to a value making it impossible
to misuse it as opposed to using bare values which can be confused by mistake.
So in addition to the obvious benefits of being able to annotate the numbers, wouldn't it be great to be able assign a unit to a string, say mark strings that are intended to be used like emails with "email"? This way you cannot use it in place of
a string marked "firstName".
Something like this:
var myEmail = "email@example.com"<email>;
var myFirstName = "Mathew"<firstName>;
myFirstName = myEmail; // <-------- compilation error
This is additional type safety feature that contribute to the quality of the code yet doesn't have a footprint on the target code (units will be type-erased just like every other type annotation in TypeScript)
A clumsy alternative to units are wrapper classes which are harder to maintain, come with an noticeable performance overhead, bloat the type system and don't solve the problem, because a wrapped value still can be misused.