TypeScript is intentionally and strictly a typed superset of JavaScript with optional type annotations that will be erased at compile time to preserve the runtime behavior of JavaScript. Currently ranked 4th on GitHub according to The 2022 State of the Octoverse.
TypeScript has been around since 2012 and you’re still reluctant to use it today. Is TypeScript really necessary? Is TypeScript better than JavaScript? If that’s the case then this post will help you make an informed decision before migrating to TypeScript.
Pros
TypeScript inherits major pros of JavaScript, but also offers additional benefits coming from static typing and other concepts specific to TypeScript. They prove especially useful when it comes to large sized codebase and distributed teams working on the same project.
- Static type checking - The ability to find bugs at compile time using static analyzer, you can fix most static analysis errors by adding type annotations.
- Self documenting - Code is easier to read because you can rely on a value actually having the specified type.
- Better refactoring - When you change one piece of code, the type system can warn you about the other pieces of code that just broke.
- Rich editor support - Major editors like VSCode and Webstorm comes with TypeScript support built in, others through plugins. Expecting features like code completion, code snippets, auto imports, syntax highlighting, bracket matching, folding, and more.
- Gradual adoption - A valid JavaScript program is technically valid TypeScript. You can migrate to TypeScript gradually and adopt the level of strictness that suits you.
Cons
Coming from another language with advanced types to TypeScript can be really fast. The only real downside of TypeScript is that you need to spend the time to learn how to use it correctly.
- Unsound type system - A sound type system means you can never get into a state where an expression evaluates to a value that doesn’t match the expression’s static type. Once your code is compiled, the resulting plain JavaScript code has no type information.
- Steep learning curve - Too steep to learn if just considered as a static type checker. But also not good enough to be considered as an indepedent programming language, you can’t learn TypeScript without learning JavaScript.
Faqs
I was very skeptical about TypeScript, I read tons of blog posts and questions on Quora and Stack Overflow. Here are my answers to TypeScript FAQs with my own perspectives after many years using it.
- Should I learn TypeScript as my main language? - No. TypeScript is not a complete independent programming language, more as a JavaScript flavor or static type checker, transpiled to JavaScript, and depends on JavaScript ecosystem. Better learn it after JavaScript.
- Is TypeScript dead? - No. It’s very much alive and growing very fast.
- Is TypesScript worth it? - Yes. Self documentation and code completion are two main reasons TypeScript being used heavily in enterprises.
- Is TypeScript really necessary? - No. It’s designed to be an optional tool to JavaScript ecosystem, you use it when you need it.
- Is TypeScript better than Flow? - Yes. Flow is fading away.
- Is TypeScript better than CoffeeScript? - Yes. CoffeeScript is fading away.
- Is TypeScript better than JavaScript? - Strickly speaking No. TypeScript is not even in the same category to be compared with JavaScript. If you look closely enough at TypeScript, it’s just a static type checker.
- Is Kotlin going to replace TypeScript? - No. Even Kotlin code can be transpiled to JavaScript, Kotlin syntax and ecosystem are designed to be more interoperable with Java than JavaScript.
- What will be the future of TypeScript? - Very bright future ahead.
- Will TypeScript kill JavaScript? - No. But JavaScript can fade away more as a compile target than a day-to-day programming language.
- Why do people love TypeScript? - Better developer experience.
- Why do people hate TypeScript? - Not really safe at runtime.
Myths
- TypeScript eliminates unit tests - It actually reduces the need to create unit tests which check the structure of static data received, these unit tests are tedious and can require significant effort to update when the data structure needs change.
- TypeScript eliminates runtime validation - All type annotations are stripped off at compile time, you still need to validate dynamic data to safe guard your program at runtime.
- TypeScript helps us refactor with confidence - Better refactoring is overrated, tests are something actually help us refactor with confidence.