On TypeScript and ass-saving

I might be Mr. Obvious today, but please bear with me.

When I started learning Javascript in the context of writing server-side Node.js code, I really enjoyed the simplicity and the lack of initial work that has to be put in to get a quick server running. The complete dynamism of Javascript turned out to be a double-edged sword, however.

A while ago I started working on a server for my yet-unreleased app; written in Javascript, running in Node.js, a basic Express server and blah blah blah. All went fine, when I was adding endpoints bit by bit and doing simple Redis queries. But when the code started growing, so did the overhead of remembering everything. Here I’m talking about strong types, or rather the lack of them in Javascript.

Writing a three js file server is a nice demo material, but once you start building thousands of lines of code of Javascript, parsing crawled data, talking to the database and doing all that in the always-asynchronous world of Node.js (where everyone and their uncle take a completion block with a different signature), shit starts breaking, because you brain starts overflowing.

Just when I was starting to feel good about the code I was building, it really started breaking, because mismatch of parameters passed into functions is no worry for Javascript - it doesn’t give a damn. Which is nice in some scenarios, I’m sure, but a complete pain in a large project.

A friend of mine told me about a strongly typed superset of Javascript called TypeScript a couple of years ago. And now I remembered, so I checked it out.

It saved my ass.

TypeScript is a gift from the heavens to me. It’s a way to write Javascript with strong types - classes, interfaces, functions with return values and most importantly, type inference (not as good as Swift‘s, but right now there’s no way to run Swift on Linux). It took me a couple of hours to set it up in WebStorm (mostly because I had no idea how it worked then, so I learned as I went - now I can set it up in minutes), but now I have proper debugging and a compiler that actually cares whether the code I’m writing will work as expected when I run it.

The way it works is pretty elegant, actually. You write your TypeScript source code with all the nice features in a .ts file. Then, before running, you need to transpile from TypeScript to Javascript. The end output is still valid Javascript source code which is what you end up running, but the TypeScript transpiler uses all that type metadata, saved in .js.map files. That is the metadata which enables the extra type checking, and it’s stripped when regular Javascript code gets generated (basically, for each .ts file a .js and a .js.map file get created). Thus it’s the transpiler that’s giving you the extra safety, not a modified Javascript engine. Which means that you don’t introduce any extra dependencies by using TypeScript - you just slightly modify your build & run scripts to transpile before running and add TypeScript to your package.json, to include it in your modules. That’s it. Elegant :)

I only started getting into TypeScript, but I already rewrote a couple of files into it and even the code looks very nice. Sane object creation and type checking, which, with the help of WebStorm’s autocompletion, makes my development many times faster and safer than previously with just plain Javascript. Give it a try yourself in a playground here.

I’m a noob to TypeScript, but I’m already a fan. And if you ever think about writing a large server-side codebase in Javascript, don’t even try it without TypeScript. It’ll save you ass, just as it saved mine.

Mr. Obvious out