The biggest feature that TypeScript brings to the table is static types. But, what are the advantages of static types over the dynamic typing in regular JavaScript?
A very common type of bug in JavaScript and dynamic languages happens when you pass a variable with the wrong type or the wrong structure. Those kind of errors are hard to catch before running the code. Also, the more complex the code is, the hardest it is to find those bugs.
Strict typing practically eliminate the entire category of bugs. When you create methods and functions, it forces you to specify the data types for the parameters and you’ll get errors if you call those functions using parameters with the wrong data types. In most code editors and IDEs, you’ll get the error messages as you type.
It also makes the code more “self-documented”, as the type information makes it easier to understand how functions, classes and other structures work. If you have to refactor code or remove technical debt, this makes the task easier.
Another advantage of static typing is that it integrates better with autocomplete tools, making them more accurate and reliable. That makes you write code faster. Static-typed code is easier to read for humans and for robots!
However, if you’ve never used a statically typed language before, it takes time to get used. At the beginning, it may feel limiting because you lose some flexibility. For example, you can’t combine variables with two different data types that easily. But you can get used to it with enough practice.