Why should you use TypeScript?
By EKbana on December 21, 202210m read
Why TypeScript then?
According to researchers, TypeScript detects 15% of common errors at the compile stage. Although this is far from a 100% result, this is still a significant amount to let developers focus on resolving logical errors rather than making them stuck catching common bugs in their code.
TypeScript is also very good at automatically inferring the types of new variables. When we say const firstName = ‘prajwal’, it knows that firstName is a string because of the value I assigned to. In these cases, when writing a “script”, the TypeScript compiler can help prevent errors.
Here are some good reasons I feel why you should use TypeScript.
Object Oriented Programming (OOP)
TypeScript supports OOP concepts such as Classes, inheritances, interfaces, Method Access Modifiers, and so on. which makes it easier to build well-organized, robust, clean, and scalable code. This advantage becomes more obvious when our project grows in size and complexity.
They are a good way of specifying a contract.
They help to provide compile-time checking.
They enable new objects to take on the properties of existing objects..
They control the accessibility of the members of a class. It has 2 access modifiers - public and private. By default, the members are public but we can explicitly add a public or private modifier to them.
Optional Static Typing
If our system uses the above function to add up interests in the principal amount of their customers and update their accounts automatically. We are assuming here that this function is taking numbers as arguments and should work without any issues flawlessly. However, due to some issues in our backend, we are getting a list of interests as strings instead of numbers. Can you guess what could possibly go wrong here? Let's see with an example:
The result is 25000 as a string instead of 2500 as a number. The customers would be really happy that they got an additional 0 at the end of their principal amount instead of getting no interest at all. The above program can be implemented in TypeScript in the following way:
Hence we know that in JS (dynamically typed language) there is no built-in static typing, so even if we create this function intending to add numbers, JS would not know that, and unexpected scenarios can take place. But in typescript, we can explicitly define the function parameter type which prevents us from passing the wrong type of argument to the function and prevent certain issues already at the software development stage. The biggest system can be broken by the smallest bug and typescript helps to fix this issue.
Static typing is not only about catching bugs but also gives the code more structure, makes it more readable and self-documenting, and speeds up debugging and refactoring. It increases productivity across a large team. TypeScript does not force us to use types everywhere so we have that flexibility and we are free to change the level of type strictness in different parts of the application.
Readability - TypeScript documents itself
Let's assume that we have a function called renderProducts which takes products as an argument.
- If exists, we can read the documentation about this function.
- We can console.log(products); and run the application to check the “products” arguments.
- We can ask our teammates if someone has been recently working on it.
- We can track what data is put onto it after finding where the function is used.
- Though this is not the best idea, we can just assume that the product's argument is what we think it is.
With all these options to find out what we are looking at, in a complicated codebase, it would be really difficult to stop what that argument is and what type of data it contains. But we typescript, we can see what that argument is and what type of data it holds directly in our IDE or the compiler when something goes wrong. The same function above would look like the following in typescript:
Without even reading the whole function definition, we can know that the argument of the product is a product type array and what the product consists of.
After the TypeScript compiler translates the TS code into vanilla JS, every device, platform, or browser that runs JS also works with TS. Usually, a built-in TS compiler is present in the IDEs and editors supporting TypeScript. Similarly, it’s fully compatible with ES6. This assures to have smooth switching and language portability.
Rich IDE Support
We, developers, spend most of our time in some IDEs such as IntelliJ IDEA or lighter code editors VSCode writing different programs. These IDEs make our lives easier and faster rather than writing code in a simple notepad which would be more painful and can consume far more time. Some features that Typescript provides include mouse hover support, code auto-completion, real-time type checking, easier code refactoring, and much more.
I hope this article gives you some insights into why you should use typescript in your future applications.
Happy Learning! Happy Coding!