Saturday, 6 October 2012

Side Note on TypeScript

Introduction

This is a slight break from the usual random short stories and bad poetry, but I thought I'd add my view on the new TypeScript programming language from Microsoft.  My work-time life is spent developing websites and database systems and I had the pleasure and privilege to meet Mr. Shanku Niyogi, Director of Test from the Visual Studio team at Microsoft yesterday.  Shanku was the guest speaker at a VBug organised Visual Studio 2012 Launch Event at Microsoft, Reading, UK and spoke on several topics in and around Microsoft's new IDE.  One of the topics that came up, and one which Shanku spent some time on, was the subject of TypeScript.



What is TypeScript?

Well, in plain terms, TypeScript is superset of JavaScript (or, more accurately, ECMAScript - Shanku was explicit about the support for a pending release of ECMAScript...  However, for simplicity and ease of writing, I'll simply use JavaScript throughout the rest of this article).

What I mean by superset is that, where JavaScript is a powerful but loosely-typed scripting language, TypeScript is exactly the same, but with the added benefit of allowing full strong typing, classes, interfaces (which can also behave in a similar way to structs) and modules.

Exactly the same?  Well, yes, essentially if you take any .js file and paste the contents into .ts file, it will work as TypeScript.



How does this work?

Here's the key: TypeScript is actually compiled to JavaScript.  Whereas you might compile a C# class to an IL DLL, or a C program to assembly, TypeScript is actually compiled to JavaScript.  If you want a demonstration of this in practice, hop over to Microsoft's TypeScript Playground (currently in preview (6th Oct 2012)).  As you type your TypeScript on the left-hand-side, you can see the resultant JavaScript being produced on the right-hand-side.

The real beauty of TypeScript is that it really is, to all intents and purposes, JavaScript.  

This means several things:

  • By making TypeScript compile to JavaScript, the Microsoft team has avoided the need to update any browser technology
  • Any existing JavaScript will work as TypeScript, because it will simply compile to itself (minus comments and some whitespace, and perhaps a few other bits)
  • There's a minimal learning curve for the new language.  In fact, as someone who's worked with C#, C++, Java and JavaScript over the years, I'd say it'll be easier to pick up TypeScript than it was to pick up JavaScript because TypeScript's inherent support for things like classes and modules is so similar in syntax and structure to those other mainstream languages.  Yes, JavaScript's approach works and is really powerful, but TypeScript bends this and shapes it into something we're all already used to.  And, here's the thing, if you don't like TypeScript's way of doing classes, you can still type JavaScript in your TypeScript files if you want and get the benefit of the type-safety elements and all the other IDE features that come with it.

But why bother?

OK, well Shanku explained this really well, so I'll just steal his example (I hope you don't mind, sir!).

The Microsoft team saw a problem.  

  • JavaScript is a really powerful and really popular scripting language.  Check!
  • The latest web browsers have refined the way script is managed to such a degree that websites can now use a large amount of script without much of a penalty.  Check!
  • Support for JavaScript, especially within web browsers, has reached a point where it is nigh-on universal.  Everyone has support, so everyone uses JavaScript.  Check!
The above points lead to one critical point: More JavaScript is being written now than ever before (and the complexity of that JavaScript is, perhaps, much greater than it was in the past).

Side Note: Windows 8 apps are written in HTML and JavaScript.

The problem is, JavaScript is a weakly-typed scripting language.  By weakly-typed, I mean that a variable can be any type at any time, effectively meaning that all variables are of type "Any".  JavaScript also doesn't make it easy for developers who are used to an Object Oriented class-based approach to their code structure, although it is possible to emulate classes through the use of function variables, as demonstrated by the default "Greeter" class in the TypeScript playground.

Finally, and largely due to the weak types, it's difficult to provide IDE features such as intellisense for JavaScript.

In the past, you may have typed up your reams of JavaScript in C#, in order to gain the benefit of strong typing, debugging, unit testing and IDE functions such as refactoring and intellisense.  However, doing this would run the risk of introducing bugs where there are differences between the languages.  Shanku used the following example:

Q1: If you divide 5 by 2 in JavaScript, what do you get?  
A1: 2.5 
Q2: If you do the same in C#, what do you get?
A2: 2

The point is, TypeScript sits above JavaScript - it is JavaScript.  But using TypeScript to create your JavaScript files gives you all the power of a fully-featured high-level programming language during development, whilst keeping all the benefits of the actual JavaScript itself.

And that's essentially what you're doing - I'd like to draw an analogy between using TypeScript to create JavaScript, and using a WYSIWYG editor to produce HTML.


OK, So what do I get for TypeScript that I don't get for JavaScript?  Really spell it out for me!

If you use TypeScript to create your JavaScript, you will get the following benefits, over simply typing out the JavaScript from scratch:


  • You can strongly type your variables in TypeScript, so the IDE will throw up warnings and errors during development that would otherwise have been missed (and possibly caused bugs) in JavaScript
  • You can define full classes, complete with private variables, in TypeScript, giving you, the developer, more control over how your class and variables are used by other developers or yourself. Note: When converted to JavaScript, any private variables will, of course, be as accessible as any other property, but the thing is, by the time you compile the TypeScript application, the support for private variables has done its job
  • You can define descriptor files for well known or in-house JavaScript libraries, such as jQuery, that will give you full intellisense in Visual Studio when developing against these libraries.  There is almost certainly already a descriptor file available for all the main libraries
  • Saying that, you get proper intellisense!
  • You can right-click on any property, function, class name or variable in a TypeScript file and select Refactor -> Rename from the Visual Studio IDE to rename that element
  • You get proper support for testing

There are probably many more benefits that I can't think of at the moment.


What TypeScript is not...

I hope that the above is clear enough to alleviate any concerns about TypeScript.  However, I thought I'd put a few points down here to clear up any worries.  Some of these points were raised at the demo last night:

  • TypeScript is not a replacement for JavaScript - far from it, TypeScript has been created to make writing JavaScript easier (so hopefully even more JavaScript can be written)
  • TypeScript does not impact your application's performance.  Because all the type safety features, meta information, class structures, etc. are all TypeScript-specific, these are all converted into bog-standard JavaScript when the TypeScript is compiled, so you're not "running" the TypeScript, you're running the JavaScript, just as though you'd written it from scratch.  I guess it's possible there may be minor performance differences between hand-written implementations and TypeScript-produced implementations of the same thing, but I'd suggest it's more likely the hand-written JavaScript would be worse performance-wise, especially as TypeScript is refined by an open source community over time
  • TypeScript will not make you more attractive to women.  However, if you use the extra time you have on your hands as a result of making an HTML/JavaScript application in record time (and with the safe knowledge that you have no memory leaks and no type-safety bugs) to go on a run each day, the end result of you being more physically fit and healthier on the inside may make you more attractive to women on the outside.  Without a proper study, it's hard to say for certain the degree of benefit.

Summary

You can probably tell, I'm a big fan.  Another thing to mention is that TypeScript is completely open-source (it sort-of had to be, because it's written in JavaScript, so it's easy to read and adapt. But I'm glad the Microsoft team chose to make it official).  

What TypeScript does is it enables JavaScript to be written in a much more structured, safe and, above-all, developer-friendly way.  And, at the end of the day, it's still JavaScript.


If any of the above is incorrect, please feel free to comment or contact me directly on Twitter (@LordScree).  I'll aim to make any corrections necessary within a reasonable time.



No comments:

Post a Comment