May cause drowsiness

Menu

Adding type annotations to JavaScript

A few people have mentioned that there is value in TypeScript for the developer to express his/her intent while writing code. e.g. “I want you to pass a string as the first parameter”. TypeScript, and ES6, do this by adding in an optional type annotation.

function foo (bar:string)
{}

Given my assertion that the only way you gain any benefit from type checking is by writing ALL of your client code in TypeScript, what’s to stop us from adding that type annotation now? Take the TypeScript code from before and specify the type like so:

function foo(bar/*string*/)
{}

Ugly? Yes. But it has the benefit of being stripped out if you minify your JS before deployment while not requiring you to use a compiler and still communicates the intent.