Why Do You Need TypeScript?
Why is TypeScript:
JavaScript is a funny language, there are no two ways about it. but what makes it so funny?
It was created years ago without accounting for the scaling it can go through
This makes it difficult to handle the complexity provided by modern web
But at this point, JavaScript has exploded as a language and won't be stopping anytime soon
It is used for web, mobile, server and so much more, this brings a fair share of problems into it
Some of these problems are:
Why is JavaScript so weird?
This is primarily due to being a client-side language, it was never intended to work outside of the browser
Being a client-side language, it interprets its storage and function during run time, introducing another big problem
This means until a certain line is hit, errors on it will be figured out
Imagine you want an integer to be sent to a function but you forgot to parse the string, JS will show you no error for this making it difficult for developers to figure out what went wrong
What is a Type?
Before understanding what is TypeScript, One needs to understand what is typed
Type refers to declaring an input/output object type before running it. Languages like C++ and Java are strong types
Think of this as a security check for data
JavaScript on the other hand as we discussed earlier is a weak type, which means no checking of data type is done at any stage
The only way possible for this is to run into a Type Error which happens during run time, again increasing the amount of debugging
What is TypeScript?
TypeScript is sort of an add-on for traditional JavaScript which makes it typed. This allows the developers to make sure the behaviour of their JS code, making development time faster
The level of type can also be customized, providing flexibility
Let's look at an example:
The bellow greeter function is written in JavaScript
You can see, we are not specifying what we want to send inside the function or get back from it, this uncertainty can break your code when it has multiple API calls
This is weakly typed
Now in this example, we have implemented typescript instead of JS
As you can see we are specifying we need a string argument to enter the function and a string must be returned
This will inform the user during development if something wrong can happen, thus being strongly typed
But what if you wanted to use the function for a number as well as a string? That is also possible as TypeScript primarily focuses on DX
Using the '|' symbol we can easily add or condition for your types, making sure it will be either of the specified ones
Sometimes you need more than just having simple types like string and numbers, you have JSON objects coming from APIs
This TS allows you to make custom types known as interfaces
Take the below example of a greeter function taking an object with name and id
How to get this code running?
TypeScript is compiled, rather than interpreted like JavaScript, which means errors can be caught before execution.
So your TS code will be compiled to JS before running, and the browser won't have to learn TypeScript itself ๐
This takes place using the TypeScript compiler, you can use simple commands to do it manually but many modern frameworks and libraries will make the process automated for you
Meaning you might not have to change anything to code in TS
Note: Since TypeScript is compiled into JavaScript, your working JS code will work without any changes, for that the compilation will just ensure that the code is strongly typed
You can also change the level at which you want TypeScript to govern your program
Summary
So that's a basic introduction to what TypeScript is and what it does, what do you think about it? Will you use it in your projects?
What is the selling point for you in TypeScript?
Tell me down in the comments section what you felt about the blog โ