Why Do You Need TypeScript?
Sachin Singh Bhadoriya
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
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:
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
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?
The level of type can also be customized, providing flexibility
Let's look at an example:
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?
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
You can also change the level at which you want TypeScript to govern your program
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 ✌
Sachin Singh Bhadoriya
I am a software developer, well versed in the latest technologies and have a keen interest in web development. Currently, I am well versed in React, NodeJS, ExpressJS, MongoDB, and Python. I am a self-taught programmer and I am always looking to learn new technologies and frameworks.