TypeScript is a bicycle

The idea of strongly typing data in JavaScript is becoming a hotly debated subject. Supporters of the current status-quo, where JavaScript does not enforce types of any kind, have long sustained the notion that the dynamism of the language is one of its strong features; that you need to be able to assign any value to any variable or field with no regards for what should be in there.

I will not go into the specifics of why I disagree with that sentiment. Much has already been written about the advantages of type safety by people that are both more eloquent and more knowledgeable in the subject than me. My own little stories of how type strictness saved me time and chagrin are but a little drop in the ocean of other similar tales out there.

Instead, I want to propose a different way to look at the problem.

Consider JavaScript. Solving a problem in JavaScript, such as a feature in a website, is equal to navigating between two points in space. You start at point A, and then start walking in the general direction of point B. Along the way you may meet obstacles: you may need to climb some stairs, hop over a fallen trunk. Yet, getting to where you want is a solved problem. With enough effort — that is, a lot of steps — you will eventually get to where you’re going.

Frameworks, transpilers, and other solutions that run atop JavaScript run parallel to that notion. And to me, TypeScript is the best example of how that analogy can carry.

TypeScript is a bicycle.

For a second, imagine bicycles were invented today and we were to compare them to the natural act of walking.

Using a bicycle forces you to act in a certain (some would say, more strict) way. Suddenly, you cannot just hop over a trunk; you need to dismount, and, carrying the bicycle on your arms, hop over it. Stairs are also complicated, needing similar if not greater effort in a bicycle. If you don’t have a lot of storage space in your home, a bicycle does not help either; it is hard to store, what with its odd frame and the inability to stand up straight unassisted. Also, the skill of riding a bicycle has a somewhat scary learning curve; you’re likely to fall a few times, or worse, to look like a fool, before you’re able to ride them efficiently.

That kind of comparison makes little sense. Yet, it is exactly what people have been doing when talking about the glory of JavaScript’s current lack of a more strict type system. People have been talking about how a good pair of shoes is better than a bicycle, and for the most part, it’s as if this is coming from people who have never tried a bicycle themselves.

The reality is that under most conditions, riding a bicycle will beat walking. You will get to your destination faster, and with less effort. You may not even need to hop over the trunk, or take the stairs at some points — going around it in your bicycle will likely be faster. Some problems will never go away: you will need to store your bicycle somewhere, and some amount of maintenance is necessary. But that doesn’t change the fact that if you need to get from point A to point B with the least amount of effort, all things considered, a bicycle is likely to be the most efficient way.

When TypeScript (and, truth be told, other similar transpilers or checkers) ask you to declare the type of a variable, or when it does type inference, it’s not really restricting what you can do. You can, after all, still declare things as being of any type, or just outright leave it undeclared. It is not slowing you down; not in the long run. It is, instead, making sure you go faster by forcing you to think on where you want to go, and then not allowing your code to stray from the path too much.

If you’re working with a dynamically typed language, the next time you run into a small misspelling error that took you some time to track and fix, or the next time you have to compile and run your code just to see if your references are right, or the next time you write a test to check for something trivial, think about how much time you could have saved with a transpiler that caught those before the code was ran — or better yet, while you were still writing the code, right from your editor — with 100% accuracy.

That’s the bicycle you’re refusing to learn how to ride.