Saturday, 13 October 2012

TypeScript: JavaScript’s Equivalent to C++

I’ve never been partial to JavaScript. I’ve no doubt that in the right hands you can do some truly amazing things with it, like:

But I don’t use any of that for my day job; I simply use just enough JavaScript to get my website to work, which means I effectively treat it like a “glue” language and therefore don’t have the same respect for it like many other web developers would. I have considered trying to put aside any misconceptions I have and knuckling down to learn how I can use it effectively and make beautiful, maintainable code but at the end of the day for me it always boils down to the following problems:

  1. Thanks to it’s dynamic nature you can do anything you want to your objects. This makes it both a blessing and a curse. A blessing in that it gives you an enormous amount of power if you want it; a curse in that if you make a stupid mistake then it will merrily fail and do nothing about it – no critical errors or exceptions, just carry straight on as if nothing happened.
  2. Although not limited to JavaScript, having a dynamic language means I cannot inspect the code with IntelliSense/autocompletion, or at least nowhere near as well as a static language. I will freely admit that Visual Studio’s IntelliSense has spoiled me rotten and I simply cannot live without it now; the ability to see what functions/properties exist in a type as I am writing my code is a godsend. With JavaScript though I am forced to remember APIs or look up documentation for the DOM to remember how to do even the simplest task.
  3. JavaScript was designed to be just a scripting language yet it has been proven that with a bit of trickery you can emulate other language features, such as classes and inheritence. But for someone who does not write JavaScript every day trying to understand these concepts is perplexing. For example, did you know that there are three different ways to define a JavaScript class? How am I to know which is the correct method to use? And when looking at other people’s code does this mean I then have to do some mental juggling to identify when someone uses a different method than me?
  4. Because it is a scripting language and not really designed for application-level code there are a number of quirks that you have to get your head around, such as lack of modular structure and having both a null and undefined concept. For an experienced JavaScript developer this must be second nature but to me this could result in hours of wasted time trying to understand why my code isn’t working when it looks like it should.

But maybe my biggest problem with JavaScript is that even if I or anyone else doesn’t like it, tough! JavaScript has the monopoly on client-side web programming and there is simply no other alternative. To paraphase the famous Henry Ford quote, you can use any client-side scripting language you want, as long as it’s JavaScript.

Enter TypeScript

Now my intention for this post is not for it to be a rant against JavaScript; many developers love it, my language of choice is C#, to each their own. Rather it is about the recent news and release of TypeScript from Microsoft. If you haven’t heard, TypeScript is a language designed to add things like type annotations, proper classes and modularisation into JavaScript. I would recommend viewing two videos from Channel 9 to get a proper sense of what it is all about:

  1. Anders Hejlsberg: Introducing TypeScript
  2. Anders Hejlsberg, Steve Lucco and Luke Hoban: Inside TypeScript

What makes this interesting to me though is that it is not a language like CoffeeScript, which has it’s own syntax to learn that then compiles into JavaScript code, thereby making you a step removed from the result. Rather TypeScript starts as JavaScript code which works completely as you would expect, and then you can add such things as type annotations where you think applicable to clarify your intentions, and again it compiles to pure JavaScript. As an analogy, if JavaScript is like C, then TypeScript is like C++; backwards compatible with the existing language (plus all the millions of libraries and frameworks written in it, including probably the most important one) yet it can help you define some core language concepts much easier to make you more productive.

And for all those people who would complain that Microsoft are trying to take over JavaScript or subvert it for their own cause as they may have done in the past, be aware that nearly everything they are adding to the language is actually very closely mapped to the ECMAScript 6 standard which has already defined future features of JavaScript; TypeScript simply brings these features to you now and compiles it all away into JavaScript code that you can run today in any browser, anywhere.

So Why Bother?

So why do we need TypeScript, or CoffeeScript or Dart for that matter? Why can’t we just learn to love JavaScript? I’m not sure why exactly, I cannot put my finger on it, but JavaScript tends to just frustrate me somehow. The other day I had to make a change to a DOM event hander and spent ages trying to understand why the code I wrote – which looked fine to me – just did not work. All I had to go on was running through it over and over again, no tools could help me spell out that something was clearly wrong that I had missed.

And that is actually what TypeScript is for. It isn’t necessarily a language designed to make JavaScript better, it is actually to provide better tooling for JavaScript. Once you have a well defined type system in place, you can build tools which tell you all sorts of things about your code: trying to pass the wrong types of values into a function being an obvious example.

Not only that, but I see this also as a means to help developers new to JavaScript to ease them into some of the complexities of it. Remember how I said there were three ways to define a class? With TypeScript there is just one, which then compiles into a JavaScript equivalent. Having just one means of accomplishing a task a newcomer can then start to learn faster and, if they then want to, delve deeper into what the compiler would then produce to learn more complex JavaScript.

As TypeScript is so new I cannot say yet whether I would actually use it or not; I would need a justified work scenario to consider experimenting with it. But it’s initial premise has definitely intrigued me, because although something has to be written in JavaScript there is nothing to say that a tool can’t help you with the job in hand.