Why I Love Typescript
Tags: vscode, javascript, typescript, webdev
Published: 2019-07-26 05:28:27

I've been using __Typescript__ for over a year now. Mostly I use it in the Back-End Side in my Apps. I started using it last year due to the reason of it was more __readable__ than plain old JavaScript. Yea it was growing popular last year and now it made the top ten spot for the most programming languages. [Top 10 Languages July 2019](https://redmonk.com/sogrady/2019/07/18/language-rankings-6-19/). Take a look at the Rankings last January [Top 10 Languages January 2019](https://redmonk.com/sogrady/2019/03/20/language-rankings-1-19/) it's beaten __Objective-C__ and __Swift__ languages that are used to build IOS Apps. 
I'm not saying  __TypeScript__ it's better than those languages. Languages have different approaches on how to solve a specific problem. 

Let write some code.
![JavaScript](https://thepracticaldev.s3.amazonaws.com/i/jmqmyi07pgxtzffkdwbp.png)

![TypeScript Equivalent]
(https://thepracticaldev.s3.amazonaws.com/i/e0lxkxuj0lg5zidcabdd.png)

Now the thing after the __:__ or __colon__ indicates the type of the variable.

Different types in JavaScript.
![Types in JS]
(https://thepracticaldev.s3.amazonaws.com/i/v934rtq51cw8yhpj7057.png)


Making an array with a specific type
![Array with Types]
(https://thepracticaldev.s3.amazonaws.com/i/dkcncwz0o6gn77sxfahp.png)

In the above, examples are exactly the same but the two below we're just using the Generic Array type __Array\<someType\>__ passing the primitive type or maybe some custom type that you made inside the less than symbol and greater than symbol.


Making Custom Type or __Alias Types__.
![Alias Types](https://thepracticaldev.s3.amazonaws.com/i/p5vap4barb2y8p460jvm.png)
The __type__ keyword is only available in TS, not JS. after the type keyword, we specify the name of our custom type and after the equals sign, we indicate the value of our custom type it may be a primitive type or value or another custom type. The __\|__ or pipe operator means that our Alias Type may hold one or more several types. In the first example, our __StringOrNumber__ can only have a value of string or number. In the second example, the __BoolOrNumber__ can only have a value of boolean or number and the third example __BestGameEver__ can only have 3 string values.


Custom Types in Array
![Array with Custom Types](https://thepracticaldev.s3.amazonaws.com/i/r5dcet5v69rdd5ghc493.png)



Now let's talk about functions.
![Functions](https://thepracticaldev.s3.amazonaws.com/i/giqfsggv8q4ak3fy8w2b.png)
We can also specify types in parameters. Specifying types in function parameters is also the same with variables. The third parameter with the __?__ makes the parameter __optional__. We also specify the return type of the function after the last parenthesis and colon and in this case is an object.


My favorite feature in TypeScript is __Interfaces__.
![Interfaces]
(https://thepracticaldev.s3.amazonaws.com/i/j7do74r64xa33v8j4tja.png)
Interfaces describe the shape of an object it tells that our object that it must have inherited properties that the interface has. We can also make an optional property by adding a __?__ after the property name. 


We can also use that interface to modify our return type in our __Response__ function earlier.
![Interfaces]
(https://thepracticaldev.s3.amazonaws.com/i/j4gebywv68zrihhvjluz.png)



My problem with __JavaScript__ is that it's not readable. I'm not saying I'm Dumb or whatever. I can read 100 or 150 LOC of JavaScript. But Understanding it takes time. I know we're programmers we spent a lot of time reading and understanding code that's part of our job. __But in a given scenario. Some error occurred in the production environment the language used is JavaScript and the Programmer who wrote it is not around and you have the task to fix it and the lucky part is you don't know JS only C# and Java. Maybe you can fix it with the help of a teammate who Rocks in JavaScript but what if this teammate is busy doing his/her project or Maybe you have another teammate that partially knows JS and he/she offered his/her help to you and you two spent a lot of time understanding the code and he/she told you he/she doesn't understand shit or maybe you can the guy who wrote the code who apparently is in a vacation with his family and you thought that you don't what to disturb the guy or maybe you can go [w3schools](https://www.w3schools.com/) to understand some basic JS code or maybe you can ask some help from the tooth fairy which obviously does not exist :smile:.__




####Thank you for wasting your time reading this :joy:###

__Have a Nice One__.