Header Ads Widget

Why should you use TypeScript?

JavaScript is a dynamic language, the type of variables is determined at runtime.

    var a = 10;
    cosole.log(a); // number;
    a = "some String";
    cosole.log(a); // string;

From the above example you can see that var a is changing from number to string. It is not possible in Other programming languages like C, C++, C# and Java etc. So, we can say that javascript is not typesafe. A variable can be changed to different type at runtime. This may lead to many bugs if your application is big or growing big. Statically typed language does not have to worry about its variable types, because we know that int a will always remain integer and will throw an error if try to assign a string to it, In fact it will throw an error in editor itself (even before runnig the program)

typescript with javascript

With TypeScript we can make JavaScript behave like statically typed language. At least we do not have to worry about variables types, because they are type-safe now.

Let's write a sum function in vanilla JavaScript which takes 2 argumets a and b and returns its sum.

    function sum(a, b){
       return a + b;
    sum(1, "bad argument"); 
    //output: 1bad argument

See the above example, This is not the output we are expecting. It should throw a proper error message that argument b is not of type number. Because JavaScript concat a number and a string if we apply + operator. We see the output as 1bad argument which is correct according to javascript behaviour. But in the software development industry, we need to write robust code which does only what is expected.
Now, let me can change the above function to handle the exception.

    function sum(a, b){
      if(typeof a !== "number" || typeof b !== "number")
         throw new Error("wrong innput")
         return a + b;
    sum(1, "bad argument");
    //Output: Uncaught Error: wrong input

But, don't you think that instead, we write a code for checking the type, language itself should have that capability to detect it. The programming language should itself tell us that- Hey look, Here I need number and you are passing a string.

Here comes TypeScript as a saviour for you. You just declare the type using TypeScript and it will take care of the rest of it. We don't have to write a specific type checking code. With TypeScript plugin installed in our machine, We can see a red highlighter that indicates a type error way before we execute the code.

why should we use typescript?

In the above example, you can see that editor itself highlight the wrong input. This example is very small. But, think of a real application having lots of data flowing around. Think of a reusable function which is getting accessed from different part of the application. Even a new developer will easily know what data I need pass without seeing the actual implementation. It is better to put safety nets than free fall.

When your application grows big, when you write too many reusable helper functions having n number of arguments passed to it, When you want your data to be type-safe, it is better to go for TypeScript. You may take a bit of time to learn it and master it. But, it is better to start now than later. You can write codes without TypeScript, But once you start using it, you will see that with the proper error message and error highlighters it saves lots of development time.

Post a Comment