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.

Because really, isn’t type checking in JavaScript really just typO checking? 😉

  • So, wild guess, what if you’d just swap your minifier for typescript compiler? You’d get better syntax and tooling support. You just grumpy because it’s Microsoft created that compiler.