Why to learn Typescript
Tags: typescript, javascript
Published: 2020-09-01 13:36:26

This article is originally written here with code snippets-> https://easyontheweb.com/why-to-learn-typescript/

I have been using Typescript on my personal projects for around 2-3 months now and I must admit it is worth the time invested in it. Due to the positive impact it has had one my development I wanted to write this article to share my experiences with Typescript and why I believe you should probably learn Typescript especially if you are a Javascript developer.

What I think of typescript as is a superset of Javascript which has all the features of Javascript but many more. Typescript can actually be thought of as a language + tools that give us extra security and checking during development. But at the end of the day, all Typescript compiles down to Javascript which is then used.

Typescript vs Javascript
As I mentioned in the last paragraph, all Typescript eventually compiles to Javascript and our applications or the environment our applications run on never know that they were written in Typescript and not Javascript.

Let’s see how Typescript differs from Javascript in some general sense of the language:-

The main difference both of them is that Typescript is optionally typed while Javascript is not. (We’ll see how this is also the biggest advantage)
Typescript supports modules out of the box whereas Javascript does not.
Typescript has better code structuring and object-oriented programming techniques.
There are a number of such differences but the ones which make all the difference I think is the ability of Typescript to be strongly typed and also the way Typescript can make use of great Object oriented programming patterns and techniques better than Javascript.

How does development in Typescript differ ?
Development in Typescript actually differs a lot when compared to Javascript. Javascript is more of a free scripting language that basically does not bind you to follow any pattern or rules. This leads to mistakes. No matter how good a developer you are – because Javascript is just in time compiled and not compiled beforehand and because it can very well accept anything anywhere, we leave ourselves vulnerable to well our own self.

In my experience with Typescript I basically liken it to something like testing, just not as intense. Even though you may feel like using Typescript is slowing you down initially, in the long run the security and to be honest the OO patterns that we use in Typescript speed up our development time than slow it down.

As I told earlier as well, Typescript is not just a language, it is what we can say a kind of ecosystem that also involves tools. So, say you are using VSCode to write your application in Typescript – the best thing then is that Typescript will inform you of your mistakes while you are writing the code. Yes, while writing the code. How awesome is that? And that is why I said eventually it will save you more time than you spend on it.

The ability of Typescript. to keep a monitor over us while coding itself and not waiting for the error or bug to pop up during runtime is what makes it absolutely amazing. I read somewhere that working with Typescript is like writing code with a friend sitting alongside you who keeps a check on any silly mistakes that you might be making and missing if you were writing Javascript.

Typescript – your friend
Typescript catches your mistakes as you make them – now, don’t think that it will also catch any logical or coding mistakes that you will make – if it did that we all would be jobless. But what it can help you with is the silly mistakes.

Take a look at the example I’ve typed down here :-


Can you see how Typescript is telling me there is an issue with book.writer ? Had this code been written in Javascript, I would have come to know about this issue only on runtime and then I would’ve realised that there is no such property as writer for the book object.

That is why typescript is your companion while writing code – Typescript knows that book has no writer property and is therefore asking you to check your code while writing it itself.

This is just the simplest example I could come up with to show how Typescript acts as an imaginary friend for you during development and trust me this saves an awful lot of time in the long run. It’s not just about accessing properties but as I told strongly typing is another thing.

Let’s see a simple example of that as well :-


Here our friend Typescript is making use of it’s strongly typed properties and telling us that we have mentioned that addNumbers should only take numbers as arguments and we are passing a string and therefore we are being given the read line under businessSeatsLeft.

The syntax is not the main focus here, actually not focus at all – I just want to show how Typescript keeps us informing of our mistakes throughout our development process and again, that saves a huge amount of debugging effort and time.

Object-oriented Typescript
Another benefit I saw of using Typescript was that of the usage of OOPs patterns. Now, this is a kind of controversial topic as you may very well use the same OOPs patterns in Javascript coding as well but to be really honest, I never did and I’m pretty confident most of Javascript devs don’t.What Typescript brings in with it’s extensive checking is that it kind of pushes us towards using OOPs concepts and of course it does bring in some concepts that are not present in JS at all like interfaces and all but what I would say is that Typescript becomes that friend of yours who motivates you to follow OOPs patterns – and actually they are great, they help you in writing DRY code and also your understanding of object oriented programming improves in general.What I would say is sure you can write applications with Typescript without leveraging any OOPs concepts but that would just lead to more repetitive code.

How to get started with Typescript
To be really honest, you would need some kind of a guide or tutorial the very first time you start with Typescript and I would recommend you to follow some sort of tutorial or book or anything you’d like on Typescript. I can recommend this one on Udemy -> https://www.udemy.com/course/typescript-the-complete-developers-guideI say so because there are patterns and anti-patterns in Typescript which I think you should know of before you begin full-fledged development on it and getting to know those and getting a hang of the syntax as well helps a lot as compared to if you would just dive into it on your own (that is what I prefer for learning most things though).Just for the first time, take a little help for the experts and I think by the second time you can follow the approach of learning as you create.

If you want to check out the best free resources to learn Javascript in 2020 do check this article out -> https://easyontheweb.com/best-ways-to-learn-javascript-for-free/ 

Also, please join Easy on the web facebook group to mingle with other devs like you and me -> https://www.facebook.com/groups/503230450489995