Why use TypeScript for web development?
In the recent years there is a debate between if JavaScript needs or not the typing being a standard in the code. If we see the State of JS (JavaScript Flavors), TypeScript has gained a lot of popularity in the recent years giving at least 50+% of devs users being happy and determinated to use this superset of JavaScript in the future.
What is TypeScript?
Typescript borns from Microsoft as an extension of JavaScript which main objective is adding the type support to JavaScript.
Let's be clear, TypeScript isn't another new language you need to learn, is still JavaScript but with superpower following the EcmaScript standard, so don't need to be worried if your code won't work or not in a old broswer.
Let's think in the next example
Some basic TypeScript
let foo = true
// some code later
foo = 3.1416
if (foo === true) {
// more code needed but it wont get in and your app is broken now
}
As you see we have a foo
const with a boolean value but for some ready you change it into a number, after that your app is broken because a condition needed it won't work after that change and can be unnoticed (every one of us suffered for this), so, what is TypeScript adding to avoid this issue? Let's find out:
// We add the value type by adding : after the var name, in this case is <varName>: <type>
let foo: boolean = true
// some code later
foo = 3.1416
// here TypeScript will alert you saying that you can't assign a number type value into a boolean type, of course we need to remove this line to successfully continue developing
if (foo === true) {
// your code will be executed safely!
}
So, what TypeScript offers?
Of course is not only about typing but we can say is one of the biggest features, TS has a lot of features that makes your project modular and easy to maintain, and I can say with all certain no neccesity of adding too much documentation into the code because is almost self explained.
TypeScript offers:
- Basic Types (number, boolean, string, array, etc)
- Interfaces
- OOP Classes
- Generics (this one is super powerful that we are going to check in a future post)
- Enums
- and many more
If you wants to learn TypeScript you can check the documentation and there are some many platforms teaching you how to TypeScript like PluralSight and Platzi (for spanish speakers)
Top comments (4)
In the last couple of weeks I've become a TypeScript champion. My company just switched over to it and it's much better than plain JS.
Of course but is not about being better than plain JS. Remember TypeScript still is JavaScript but with super powers!
Oh, absolutely. But, it makes it so much easier to work with. I like that you can still use regular JavaScript within it. In fact, yesterday we solved a problem by writing out some pure JavaScript in the middle of a TypeScript function call while developing an Angular application.
Typescript ♥️