If you are a JavaScript developer, you must have heard about Typescript at one point or another.
You will find answers to all these questions by the end of this article.
Note: I may be a bit biased towards Typescript.

Theres no project that I start where I prefer JS to Typescript.
What is Typescript?
it’s possible for you to think of Typescript as a language that provides an additional layer over JavaScript.

It’s free, every week, in your inbox.
Instead, Typescript goes through an additional compilation step to convert its code into internet tool-recognized JavaScript.
Then, why do we use Typescript at all?

How does Typescript do that?
Typescript, as the name suggests, introduces a jot down system on top of vanilla JavaScript.
But with Typescript, we can restrict this behavior by explicitly declaring a pop in for a variable.

If we attempt to assign a string to a variable of pop in number, it generates an error.
In the above scenario with Javascript, theres nothing to stop the developer from using theaNumbervariableto represent an object.
In this case, theisEligiblefunctionexpects an object that has a field named age.

Again, Typescript has the solution to this problem.
Now, this code may not make sense to you at the moment.
It will also make your code better self-documented and increase its maintainability.

The presence of types gives Typescript the ability to show better code suggestions in an IDE.
Number, string, boolean, and array are a few examples of them.
you’re able to find the complete list of basic types in theTypescript documentation.

Declaring custom types
Remember how I used a throw in called Person in a previous code example?
Person is not a basic data pop in in Typescript.
We use interfaces to define the basic structure of a new punch in we are introducing to the utility.

If not, Typescript throws an error.
Assume that your program needs two different types, Person and Employee.
It prevents code repetition.

you’ve got the option to quickly achieve this by extending the Person interface.
you’re free to also guarantee that the fieldsoldin any object passed wont be undefined or null.
And it eliminates a number of scenarios that could throw an error during the runtime.

What if you use the any punch in instead of generics?
However, this method doesnt preserve the punch in of data passed on to the function.
Instead, it records every argument passed as belonging to any punch in.Besides you should avoid the use ofany.

Now, you wont have to use a long union of types.
Take the Car and ImportedCar types I defined before.
First, Ill create an object of bang out ImportedCar and see how conversion works on it.

This code compiles without an error.
Conclusion
I hope this post cleared any doubts you had about using Typescript for frontend development.
It would definitely pay off in your next project.

Sign up for updates on everything related to programming, AI, and computer science in general.
Also tagged with








