TypeScript - The uncontested JavaScript flavor

Updated on Apr 15, 2021typescriptjavascript

TypeScript is an open-source language which builds on JavaScript, most commonly used programming language on earth, by adding static type definitions.

“TypeScript is JavaScript that scales. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.” - typescriptlang.org

Maintained by GitHub owner Microsoft, TypeScript has climbed from seventh place in 2018 and 2019 to fourth overall in 2020 according to Octoverse. TypeScript is also the dominant JavaScript flavor since 2017 according to Stage of JS 2020, followed by PureScript, Reason, Elm, and ClosureScript.

Using TypeScript unlocks following sound benefits:

  • Linting tool for types
  • Great for the maintainability of long-term projects
  • Take advantage of in-editor suggestions/refactors
  • Catch errors in compile-time or while working on your editor
  • Easy integration with build tools (Webpack, Rollup…)

TypeScript vs JavaScript

TypeScript has been around since 2012, offers all of JavaScript’s features, adds an additional type system layer, and built with the JavaScript ecosystem in mind.

All valid JavaScript code is also TypeScript code. You might get type-checking errors, but that won’t stop you from running the resulting JavaScript. While you can go for stricter behavior, that means you’re still in control.

Adopting TypeScript is not a binary choice, you can start by annotating existing JavaScript with JSDoc, then switch a few files to be checked by TypeScript and over time prepare your codebase to convert completely.

TypeScript’s type inference means that you don’t have to annotate your code until you want more safety.

The TypeScript team contributes to the TC39 committees which help guide the evolution of the JavaScript language. When new features have reached stage 3, then they are ready for inclusion in TypeScript.

TypeScript tends to be more verbose than JavaScript, especially when we get into more complex things like Generics. When we begin to work with TypeScript, we might get a bit lost applying several patterns like type guards.

Before migrating to TypeScript from JavaScript, you must keep following heads-ups in mind and plan your migration carefully before making a move:

  • Static typing matters more in team
  • Way more complicated than expected
  • The migration has to be gradual
  • Full typing coverage obsession is real
  • Many libraries don’t have typings
  • It’s not really safe at runtime
  • Documentation mostly in JavaScript
  • There is no going back

TypeScript vs Babel

TypeScript and Babel let you use virtually all of the new features that ES6 brings today, without sacrificing backwards compatibility for older browsers.

Babel is more like TypeScript compiler (TSC), the JavaScript code after transformed by TSC or Babel is clean, simple code which runs anywhere JavaScript runs: In a browser, on Node.JS or in your apps.

Both support some kinds of optimization out of the box at compile time. Doing all of these optimizations manually would lead to sprawling, unreadable, unmaintainable code, but by delegating this work to our compiler, we get to write nice, idiomatic code, and have our toolchain take care of the rest.

Both can be used in the same projects by using Babel’s preset-typescript to generate your JS files, and then using TSC to do type checking and .d.ts file generation.

TypeScript vs Flow

TypeScript is often compared to Flow as a static type checker for JavaScript. Both allow you to write a safer JavaScript by adding type annotations and stripped away at compile time by compiler.

The syntax differences between Flow and TypeScript isn’t significant. They both support similar types like the usual JavaScript data types, literal types, union types, conditional types, etc. Also, both TypeScript and Flow use similar syntax for type annotations.

TypeScript has more support than Flow does with libraries, frameworks, and it’s used more pervasively in apps because of that. They both provide very similar type checking abilities which retain the flexibility of JavaScript.

TypeScript definitely wins in terms of the quantity, resources, and documentation available.

Conclusion

The debate whether to use TypeScript is over, as library maintainers you should (or even must) support typings, as app developers you can get away in personal projects but often required in enterprises.

In 2021, people expect you to know JavaScript slash TypeScript not just only vanilla JavaScript. You’ll see that more often in frontend job descriptions. Many open-source projects and enterprises are migrating to TypeScript.