Posted on 1st Sep 2015
I'll get to the why and what of TypeScript, but first a toot on my own horn - I served as a technical reviewer/advisor on the recently published Learning TypeScript from Packt Publishing.
So what is that power? In essence, it consists of “syntactic sugar” — extensions to standard JS syntax — which allows the programmer to more fully express their intentions in the actual code they write. So, for example, instead of writing a lot of comments to remind (or inform other programmers) what parameters a function is expecting, through strong typing that information is built right into the TS source code, which a TS-aware IDE can use to a) help the programmer with hints and auto-completion and b) flag errors where there are type mismatches.
JS was designed originally to be simple and forgiving. You could create a variable to hold a string, then assign an array to it, followed by an object, then a number etc. etc. and JS would never complain. At the byte level, what actually passes through the CPU, the variable is only reference to a memory location with no guarantee that whatever is at that location will “make sense”. So the forgiving nature of JS becomes entrapment; the error will only become apparent at run time. Maybe. Certainly the page will not work as expected, but the reason why can be difficult to track down.
With strong typing, to take a simple example, once you create a variable and assign it a number, TS will recognize it as having the “number” type; if the programmer then tries to assign a different type, TS will flag the error when transpiling the code to JS — and a TS-aware IDE will flag it right in the code. Now, since this “error” is still valid JS code, your TS code will transpile anyhow. It’s up to the developer to decide, oops, I didn't mean that, and fix it or let it go. Which would not be “best practice” — but that’s the developer’s decision. (A better practice, in cases where you really do need to store different value types in a given variable, is to explicitly type it as “any” when first declaring it.)
This is of course just the tip of the typing iceberg, and strong typing is only the beginning of TS’s syntactic sugar.
TS brings true class syntax with support for class inheritance/extension/subclassing (call it what you will). The transpiler converts this code into prototype inheritance, which advanced JS programmers will recognize easily, with a bit of helper code at the top to support subclassing.
Another advantage of the AS3 format is that class objects are by default sealed, I.E., unlike JS objects (and classes) which are always dynamic, your code cannot add or change properties or methods at run-time. Again, CPUs like this; once instantiated, it can expect the properties and method calls of an object/class to be in the same place in memory, while with dynamic objects it has to go through a process of looking up the locations in hash tables, a hit on performance).
Interestingly, there is some movement in favour of incorporating byte code in the browser (see this Ars Technical article), because of the inherent performances, which is ironic given that so much effort and pressure has gone into removing byte code implementation in the browser via plugins (and not just Flash; Java is feeling the same heat).