DEV Community

Cover image for Why I Love Typescript
Mark Abeto
Mark Abeto

Posted on • Updated on

Why I Love Typescript

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. Take a look at the Rankings last January Top 10 Languages January 2019 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

TypeScript Equivalent

Now the thing after the : or colon indicates the type of the variable.

Different types in JavaScript.
Types in JS

Making an array with a specific type
Array with Types

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
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

Now let's talk about functions.
Functions
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
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

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 to understand some basic JS code or maybe you can ask some help from the tooth fairy which obviously does not exist 😄.

Thank you for wasting your time reading this 😂

Have a Nice One.

Top comments (0)