Is TypeScript Better Than JavaScript?

For most applications, TypeScript is a better choice than JavaScript. It makes writing correct, low-defect code easier. If you're writing a commercial application, we recommend that you use TypeScript.

However, TypeScript does add some costs. It requires adopting at least one new tool: the TypeScript compiler, which converts TypeScript code into JavaScript code. Switching to TypeScript also requires developers to learn the TypeScript language, which can be a significant task if they've never used a statically typed language before. (Our TypeScript courses can help!)

In some cases, the benefits of TypeScript don't justify the costs. For most scripts or very small applications (under 500 lines), we recommend writing JavaScript directly.

What bugs and problems does TypeScript prevent?

If you've ever seen undefined or NaN accidentally show up in a web application's UI, that was a JavaScript bug that TypeScript could've prevented. Likewise for passing the wrong arguments to a function, like passing a number where a string was expected, or passing an object where an array was expected. In TypeScript, the compiler will tell you about those mistakes instantly; no need to debug manually. The TypeScript compiler also catches mistakes like "undefined is not a function" and "Cannot read property 'someProperty' of undefined" before the code even begins executing.

Many operations in JavaScript are unintuitive, so it's easy to make mistakes. In JavaScript, 1 + true gives 2. It's easy to see that 1 + true is wrong, but it's harder to see when those values are in variables, like x + y. That kind of bug might make it all the way to production! In TypeScript, that bug is caught before the code runs, whether it's written as 1 + true or x + y. (Our TypeScript course explains this from the ground up, beginning with basic types.)

How do you switch from JavaScript to TypeScript?

Switching from JavaScript to TypeScript is easier than switching from Python to Ruby, from C to Java, or from almost any language to any other language. That's because TypeScript is a superset of JavaScript: everything that you know about JavaScript also applies in TypeScript. TypeScript only changes JavaScript by adding support for static types.

The TypeScript compiler turns your TypeScript code into JavaScript code, which can be deployed to the web. Browsers don't care whether that JavaScript was written by hand or generated by the TypeScript compiler.

Unlike most language switches, a switch from JavaScript to TypeScript doesn't require rewriting your system. The TypeScript compiler supports an "allowJs" option, which allows TypeScript and JavaScript source code to coexist. Your TypeScript code is still compiled into JavaScript, but your JavaScript code is left alone. That allows you to convert your application to TypeScript one file at a time.

You'll need fewer tests with a TypeScript codebase because the TypeScript type system can catch many types of errors. Here's our summary of the kind of tests we've avoided writing by using TypeScript. In that summary, we estimate that we'd have 243% as much code if we built a well-tested JavaScript system instead of a well-tested TypeScript system.

What are the downsides of TypeScript?

TypeScript reduces bugs and makes many aspects of programming easier. But, as usual, you'll have to pay a price to get those benefits.

First, you'll have to learn a lot of new ideas, especially if you've never used a static language before. Our TypeScript Basics course covers TypeScript from the beginning, assuming that you know JavaScript, whether you've used a statically typed language before or not. (If you need to learn JavaScript first, we have a course on Modern JavaScript for people who already know the basics like function definitions and loops.)

Second, you'll need to run the TypeScript compiler on your code, which complicates the build process. You may already use webpack, Parcel, rollup, or another build tool. All of those tools either support TypeScript natively or have TypeScript plugins available. If you don't currently use any build tool at all, you'll have to adopt one to use TypeScript. In the simplest case, you may only need the TypeScript compiler itself.

Any build tool requires maintenance, and that's true of TypeScript as well. You'll have to update your build tool configuration over time. This is part of the reason that we don't recommend TypeScript for small scripts or for small applications of fewer than 500 lines. For larger applications, including all commercial applications, we think that TypeScript's time savings significantly exceed its maintenance costs.

Third, TypeScript has limitations, just like any type system. Sometimes, you'll be sure that a value in your program has a certain type, but the TypeScript compiler won't have enough information to know what you know. You can see an example of this in our type predicates lesson. Learning to tell TypeScript what you know is a big part of learning to program in TypeScript.

Finally, TypeScript can't help you if you intentionally lie to it! If you use the dangerous "backdoors" in the type system, you lose the benefit of static types. The as type casting keyword in TypeScript is the most notable backdoor. We recommend using TypeScript's backdoors very rarely, or preferable never!

Is TypeScript worth the effort?

We listed a lot of downsides above, but that's because we should understand the trade-offs of a technology before adopting it. TypeScript is a major improvement over JavaScript, and the productivity and correctness benefits outweigh all of these costs combined.

Finally, a word on the experience that our judgments are based on. Execute Program was originally written with a Ruby backend and JavaScript frontend. We ported the frontend to TypeScript, followed by the backend. We recommend reading both of those posts to get a deeper view into the benefits of TypeScript.

A year has passed since those two ports, so we've had a lot of time to evaluate them. In our app's history, we've added 77,451 lines of TypeScript and removed 46,086 lines (as of early 2020). We think that using TypeScript has made development significantly easier, and we think that switching to TypeScript was the right choice.

Back to TypeScript Basics Course