TypeScript to the Rescue!

It has been more than a year that TypeScript has been out there. The first time I heard about TypeScript, I was like – meh! With plethora of “compiles-to-JavaScript” languages out there like CoffeeScript, Babel, Dart and what not, I felt that TypeScript won’t be able to survive.

Apparently, I was wrong! And boy am I glad about that!

For the past few years I have been working on web based projects and they are unlike the regular out of the mill products. They require a lot of JavaScript programming with little or no UI framework. As our SLOC grew to about 3000, I was struggling with keeping the project maintainable.

You see, JavaScript gives you so much power that it becomes easy to get out of discipline when programming. Yes, JSLint can help you, but they can only take you so far. I have seen programmers add a property to object anywhere they want and then use it anywhere they want. No body else in the team knows about that property except that programmer who added it and named it flag.

With the amount of support IDEs provide with JavaScript, good luck finding where and why its been used. Ah yes, IDE support! With strongly typed languages the IDE can figure out so much by itself that IDE becomes a powerful tool for you. But with JavaScript, even Visual Studio feels like Notepad!

And that’s where TypeScript shines! So I decided to migrate our existing codebase to TypeScript. Although it seemed daunting at first, but TypeScript allows you to take baby steps. Here is the steps that we followed:

  1. Rename all the existing files from .js to .ts
  2. Change all the classes (we used inheritance the way suggested by John Resig) to actual TypeScript classes
  3. Change call the anonymous function to lambda
  4. Start decorating every variables and functions with types

Now, these four steps worked like magic for us! Just the first step was able to comb out so many issues in our code! And by the time we finished up with step four, the IDE was our friend again!

Honestly, anybody who is using JavaScript out there, should switch to TypeScript as soon as possible. Because as Scott Hanselman pointed out – JavaScript is the Web Assembly Language now. And believe me, you do not want to be stuck with programming in Assembly Language.

Leave a Reply