Around IT in 256 seconds

#64: TypeScript: will it entirely replace JavaScript?

January 03, 2022 | 3 Minute Read

TypeScript is a programming language, a superset of JavaScript. This means any valid JavaScript program is also valid TypeScript. But not vice-versa! TypeScript adds a ton of features, addressing the shortcomings of JavaScript. The most important one is optional static typing, including null-safety. The fact that you can take any JavaScript code and turn it into TypeScript by simply changing a file extension is crucial. It means you can gradually start using TypeScript’s features without rewriting your whole application.

OK, but what’s the big deal with static typing? Well, did you ever come across the dreadful Undefined is not a function error? In JavaScript, everything is an object of an unknown shape. This is sometimes useful. But knowing in advance that a given variable is a React component rather than a, I don’t know, a string proved to be really valuable. Having type annotations makes reading and maintaining code so much easier. The extra hassle of creating types and occasionally fighting with the compiler really pays off.

Static vs. dynamic typing is actually a decades-old debate. No matter which side you are on, the popularity of TypeScript is astounding. As a matter of fact, all new frontend projects I know use TypeScript these days.

TypeScript also has great interoperability with the JavaScript ecosystem. Of course, you can use JavaScript packages. But also, most popular packages have TypeScript type definitions available. Essentially, even though the package is written in JavaScript, it exposes statically-typed public API. It makes using new packages much easier. The IDE gives you hints and guides you through.

The type system offered by TypeScript is actually quite advanced. It supports:

  • union types
  • inheritance
  • interfaces
  • generics
  • null-safety
  • structural typing

…and much more.

Null-safety is especially worth mentioning. Basically, TypeScript compiler (tsc for short) tracks possible null and undefined variables. Where JavaScript fails at runtime tsc fails at build tiem with a type error. Programs that don’t compile can’t fail on production.

OK, but there’s one problem. Web browsers and node.js do not understand TypeScript. They speak JavaScript. Luckily, TypeScript is not a dead language. When you want to run it, the compiler translates it into… JavaScript. First, all type information is stripped. JavaScript has no types, so it’s unnecessary. Other high-level language features are translated to JavaScript automatically. This process is technically called transpilation.

Actually, there’s a node.js competitor called Deno. It supports TypeScript out-of-the-box. However, the translation to JavaScript simply happens behind the scenes. Theoretically, a native TypeScript runtime could improve performance, but we are yet to see such an engine.

TypeScript was originally created by Microsoft. By the way, Microsoft also gave us Visual Studio Code and AJAX. AJAX being the father of fetch API Also GitHub, after being acquired, improved significantly. And Microsoft was once the top Linux kernel contributor. So I think Internet Explorer and Windows Millenium Edition should be forgiven.

Finally, to be honest, there are plenty of languages transpiling back to JavaScript. The most popular ones are Dart, CoffeeScript and Elm. But it seems TypeScript won.

That’s it, thanks for listening, bye!

More materials

Tags: dart, deno, elm, javascript, node.js, transpiling, typescript

Be the first to listen to new episodes!

To get exclusive content: