I've been watching the TypeScript project for a little over a year now, since the 1.0 release. The concept really interested me, but as is the case with many new language conventions, I wasn't sure whether or not it would catch on. However, the recent announcement that Typescript would be used to build AngularJS 2.0 gave me confidence that TypeScript was legit and here to stay. I decided it was time to start a little sample project (posted below) and explore.
While building said project and preparing to write this post, I came across quite a few "Introduction to TypeScript" articles. This is not intended to be one of those articles. They have their place, to be sure, and I used a number of them to get started on my journey. However, I want to take a different angle - I want to pose back to you, and answer, the same questions that were in my head when I started watching the TypeScript project:
- Why should you use TypeScript in your next web or mobile project?
- What makes TypeScript sweet - meaning how will it make your job easier and your project better?
- Essentially, how does it put the sugar in your java[script]? (sorry, I had to)
Here's what I liked about TypeScript:
TypeScript is excellent at helping you organize your code.
TypeScript offers the ability to create modules that really help in this area:
- Modules work like namespaces in C#. They help to group like objects together, as well as avoid name collisions with other objects and over-saturation of the global namespace.
exportkeyword restricts access to code outside the module, clearly defining what's intended for public consumption and what isn't. Law of Demeter, anyone?
- Classes within a module can be split across multiple files for simplified maintenance, readability and reuse (more on that in a moment).
TypeScript helps you share & reuse your code.
* * * * *
Here's my sample project - it's an API testing module modeled after a PowerShell script I wrote a few months back. The TypeScript module code is below; there's also a sample HTML page that shows how to execute it: