What Is TypeScript, and Why Should You Use It?
TypeScript is a programming language that is becoming increasingly popular and is here to stay. What is TypeScript used for, and why would it be the best choice for your projects? According to the 2020 Stack Overflow Developer Survey, TypeScript is the second most loved language by developers.
But something other than feelings makes developers go with TypeScript. Let’s dive deeper into the reasons behind it and see what TypeScript is used for.
Table of Contents
Strongly-typed Superset of JavaScript
What does it mean that TypeScript (TS) is “strongly typed”? Strongly typed means that variables can be explicitly declared to be of a specific type and that the type cannot be changed.
A few years ago, the lack of strong typing among JavaScript and PHP — the most popular programming languages used for the Net — was considered an advantage that lowered the entry threshold for beginner developers. But with the ever-growing project complexity, it quickly turned out to be a disadvantage that increased the risk of errors.
JavaScript is known for its non-intuitive casting rules that allow subtle bugs and unexpected behavior to slip by unnoticed. It is well-illustrated in the result of the following command:
const a = 'ba' + + 'a'; console.log(a.toLowerCase()); // returns 'banan'
As programmers, we don’t want to encounter such nasty surprises. In an ideal world, the compiler should notify us about such issues so that we can avoid a number of errors that are non-trivial and hard to track down.
Of course, TypeScript allows for much more than just guarding the compatibility of simple types. Despite all the flexibility of JavaScript and the many paradigms and programming patterns available in this language, TS allows for full type coverage of all object-oriented data structures. TS usually infers the type based on how the code works, but you can also define it yourself.
Types can be added to both parameters and the return value of a function, as well as constants and local variables. For more complex data structures (e.g., options object options introduced into a function as the only function parameter), you can define their types and requirements as separate named data types that can be reused. As a result, you get a clean code and better data structure, so everyone on the team knows how to write consistent code.
Extra language features
Besides strong typing, there are many other benefits of TypeScript. TS provides a number of features not available in JS that make developers more efficient. Several things that got stuck in the ECMA TC39 committee at Stage 2 or Stage 3 have been implemented in TS for several years now. They include decorators to add metadata to the code in a declarative way. Attempts at standardizing them in the ECMA have been going on for three years already, while they have been available in TS for around five years.
Here are some other powerful TypeScript features:
- Improved function types: TS makes it easier to work with functions because it returns “too few parameters” or “too many parameters” errors. It prevents a whole class of hard-to-debug errors.
- Access modifiers: public, private, and protected.
- Class properties: also with access modifiers.
- Interfaces: reusable definitions of the “shape” of data structures, simultaneously ordering, explaining, and documenting the code.
- Generics: enable the implementation of universal components that ensure well-defined, typed operations in many different places throughout the code.
- Enums: allow you to define a set of named constants. The compiler performs type checking to make sure you’re not using unknown constants and displays hints for acceptable values.
All the features determine type compatibility and make it easier to implement well-defined and reusable data structures, which leads to improved code readability. Plus, it speeds up the process of familiarizing a developer with a new codebase and helps prevent technological debt.
Enhanced IDE support
There’s a wide range of IDEs with support for TypeScript, such as Visual Studio Code, Sublime, or WebStorm. All of them strive to provide as much help to programmers as possible, and the developer experience is enhanced by TypeScript. In parallel with the development of TypeScript, Microsoft is working on enabling IDE integration with the compiler.
Besides syntax highlighting and type checking, such support also provides autocompleting, hints, and suggestions (e.g., allowed methods with parameters). Along with providing inline help, it enables developers to remove unreachable code or unused imports and helps them add missing imports.
Such support is an important feature. It significantly streamlines the programmer’s work and enables them to concentrate on the more difficult aspects of programming. It keeps them far away from being distracted by things that often can be solved automatically. The programmer can write code more quickly and correctly and, because compilation errors are reported directly, spend less time debugging.
Stability and reliability
Last but not least, TypeScript is developed mainly by Microsoft. Although the company got a bad reputation in the ’90s, Microsoft overcame it under CEO Satya Nadella and firmly embraced open source, and both Typescript and Visual Studio Code use this model.
Microsoft’s code is available on GitHub, so every programmer can open new issues and submit pull requests. An interesting point to note here is the number of stars (more than 60,000!) TS has on GitHub. It’s an indicator that shows not only how many people like it but also the strength of the community. You can see the popularity of the project in the Issues section on GitHub, as well as in the number of contributors on Stack Overflow who provide real help to programmers.
On the other hand, the TypeScript case shows that not all large corporations are playing catch-up for long. TS comes with a regular release cycle, and on average, every second release brings significant improvements or even new language constructs. The language is backed by a tech giant, giving TS a steady rise and a guarantee that it’s here to stay. It has garnered support from the industry and is used to build scalable applications.
Let’s summarize
One of the main benefits of TypeScript is that it eliminates a whole range of errors that are seemingly trivial but still difficult to track down. Secondly, it helps programmers write better-quality code, which prevents technological debt. Plus, you get a self-documenting code, which lowers the entry threshold for new developers.
What’s great is that you don’t have to write validation unit tests because you can debug and test code without leaving the IDE. A real win-win, don’t you think?