TypeScript: Should You Spend Time Learning It?

TypeScript is an open-source language invented by Microsoft; technically, it is a superset of JavaScript, which means the code is transpiled to plain old JavaScript. Almost a decade old, this language has moved up in popularity on GitHub; as of the end of 2019, it is the seventh-most popular language, according to GitHub’s annual Octoverse breakdown.

In order to thoroughly discuss TypeScript, let’s talk about JavaScript for a moment. There’s a meme that compares two books: On one hand, a really thick book simply called “JavaScript”; and beside it, a thin book called “JavaScript: The Good Parts.”

For many professional JavaScript programmers, this meme is pretty accurate (and pretty funny). While the language has some great features, some of them are a bit too flexible (especially around types), which means you can end up with some nasty bugs. 

Most professionals learn techniques to avoid such bugs, and are able to produce some great software in JavaScript, both on the server side (as node.js) and in the browser. But for other programmers, the problem has little to do with the programmer and everything to do with the language itself. These programmers want static typing like you find in other languages such as C++, Java, and C#. Static typing results in code that is easier to maintain and less bug-prone.

For example, no matter how good of a programmer you are, if you release an open-source JavaScript library that includes functions for other people to use, there’s not much you can do to stop users of your library from sending incorrect objects and types into your functions. Of course, you could start every single function with a “typedef” test, and throw an exception when the wrong type is sent in. But at that point, you’re basically forcing a strict typing of sorts into your code.

So why not just use a language that has strict typing built in? That’s where TypeScript comes in. Because it’s a superset of JavaScript, all valid JavaScript code is valid TypeScript code; additionally, TypeScript adds features found in other languages to enforce strict typing and help prevent bugs. Since TypeScript is transpiled to JavaScript, your code is converted to JavaScript, which can be run in node.js and any modern browser. You can use the native TypeScript transpiler, and Babel also provides TypeScript transpiling.

The features also go beyond just types; for example, TypeScript has modern class features. While JavaScript now has support for classes, TypeScript takes it a few steps further, including support for private members. Again, this helps prevent bugs.

If you do move towards TypeScript, make sure you understand the more complex language features, such as:

You can explore these in the official documentation. Be careful; if you’re already a JavaScript expert, don’t back yourself into a corner by abandoning JavaScript altogether (unless you really don’t want to use traditional JavaScript ever again). Eventually, you might feel like you need TypeScript and are unable to do your programming in traditional JavaScript, meaning you’ll likely need to remove JavaScript from your resume.

But it’s worth maintaining your JavaScript skills; jobs in the language are abundant. It’s not likely JavaScript is actually going to go away; it’s also not likely that the features that make TypeScript unique (its typing system in particular) will make their way into the JavaScript standard. As such, both languages will likely remain—and by mastering both, you’ll open up more opportunities.