TypeScript introduces the much needed typed variables. There are 7 types:
Assigning a value of a wrong type to a variable would result in a compilation error. This allows early bug detection at compile time instead of run time and could save much time and nerves.
The types can also be declared for function arguments and return types:
Arguments can be marked as optional or have default values:
Interfaces are a real gem in tidying up your API communication. It’s a way of describing your own data structures and using them the same way as the basic types:
In TypeScript there is a more direct way to implement classes and inheritance:
Modules offer a comfortable way of organizing your code. They allow enclosing all the internal functions and classes to make accessible externally only the selected aspects:
The stricter compile-time validation is normally a blessing, but what about working with external non-TypeScript libraries or APIs? Even the simple jQuery calls would fire compilation errors galore.
cannot find name "$"
The error doesn’t abort the compilation and the .js file is still created, but what’s the pleasure of writing error-red code? Fortunately TypeScript has a fast way of integrating non-TS libraries: the declaration files. The .d.ts files allow to describe the shape of the library for compilation purposes. You can write your own descriptions, or find them in the ever growing base on http://definitelytyped.org/.
By including a line:
The errors are gone, and we’re able to fully use the jQuery functions.
The .d.ts files are development aid only and are not included in the compiled .js file.
- real time validation
- better code completion
- refactoring across many files
- navigation to definition
- tips and quick-fixes
- downloading and including .d.ts files when needed
Non-Microsoft tool support includes plugins for Sublime, Webstorm and the compilation can be attached to your Grunt or gulp process.
TypeScript VS ECMAScript6
But wait! Default function parameters, classes and modules are the highlights of ECMAScript6. With the wide adaptation of the standard just around the corner, you might ask “why learn something else?”. The best answer is: it’s not something else. Microsoft aims to align their features with the Harmony standard making TypeScript more of a bridge allowing to start using the new syntax. In that way it’s more like the modern ES6 transpilers: Babel and Traceur then Dart and CoffeeScript. In fact combined with core-js polyfil, TypeScript is the third best option for using elements of ES6 in production today, with promise of better compatibility in future releases.
TypeScript isn’t the one solution to rule them all. It would be hard to persuade Ruby enthusiasts to abandon CoffeeScript, and employing TypeScript to writing small animation plugin might be an overkill.
TypeScript is best suited if you:
- have background in C# or Java
- work in Microsoft environment
- write large scale application where loose typing caused errors before
- intend to use typings
- believe in IDE error detection
- are curious about ES6, but not fully convinced yet