You Need to Learn TypeScript Now!
Why TypeScript is a revolutionary language for JavaScript developers
TypeScript or JavaScript? Semicolons or no semicolons? Tabs or spaces? These are all common, controversial questions that are present in the programming industry. In the case of tabs and spaces, most definitely tabs. When writing code, you should unquestionably use semicolons. But should you use TypeScript rather than JavaScript? The answer is YES. Let’s take a look at what TypeScript is and why you should actually use it.
What is TypeScript?
TypeScript is a strongly-typed superset of JavaScript, making it more related to the Java programming language. The term “strongly-typed” essentially means that you can specify the types of variables and objects. For instance, take a look at the below example:
let age: number = 15;
In the example given above, a variable is being declared and assigned to a number. However, unlike JavaScript, we can specify the type of the variable that we have created by following the name with a colon and the type. If the value of the age variable were to change to a string, an error would be thrown. Unlike the Java programming language, TypeScript's strictly/strongly typed feature is optional. This means that any JavaScript code is valid TypeScript code. However, by not using the optional static typing feature that TypeScript provides, there is no use in using the language.
Not only does TypeScript allow you to specify the types of variables, but it also allows you to create your own types, as well as catch errors in compile time rather than run time. These are just a few of the features TypeScript possesses, and these features assist developers greatly, making it a language to most definitely learn.
Type Safety
One of the most useful and important features that TypeScript has to offer is its type safety. With this feature, you can add types to variables, or even create your own types. For instance, if we were expecting a string from user input, but for some reason, a number gets passed, it will not work because we wanted a string and not a number.
We can create custom types as well. For instance, take a look at the below example.
type AMOrPM = "AM" | "PM";let morning: AMOrPM = "AM";
let evening: AMOrPM = "PM";let random: AmOrPm = "random"; // Error will be thrown
We created our own type with the TypeScript reserved keyword “type” followed by the name of the type. We set it equal to the string “AM”, and used one pipe symbol to represent “or”. We followed the pipe with a string of “PM”. Now, any variable with the type AMorPM can only hold the values of “AM” or “PM”. Any other value will result in an error being thrown. We can also type objects and arrays with types too.
Error Handling
Imagine that your feature written in JavaScript was getting deployed onto your company’s servers. But “oh no”, you didn’t add a check to make sure that the user inputs cannot be numerical. You are unaware of this because JavaScript will only throw you an error at the runtime when the user actually sends the input. This small error can crash the entire server and can lead to devastating results. If you had used TypeScript, however, this error would have been caught during the compile time. The deployment would have failed, and you could have quickly fixed the bug.
Although the above example might seem very unlikely to happen, there are countless other scenarios in which TypeScript’s error handling saves the day. This feature helps out greatly in the process of finding and fixing bugs/errors. In addition to this, when using TypeScript, IDEs add red squiggly lines underlines of code that will throw an error, or add the line if they are syntactically incorrect. For instance, take a look at the below example. Since we have typed the variable “myName” to a string, we cannot assign it to a number. Therefore, a red squiggly line is underneath the part of the code that will throw an error.
Learning Curve
Since TypeScript is almost exactly like JavaScript, there isn’t much that you need to learn. For this reason, you should use TypeScript. It already provides type-safety, compile-time error handling, and great IntelliSense in most IDEs. Spending a few days to learn TypeScript to ensure that your applications do not crash in runtime due to errors is a great deal.
TypeScript is Advanced JavaScript
TypeScript is literally just JavaScript, but with additional game-changing features. If you are a JavaScript developer, you technically know TypeScript because all TypeScript code is valid JavaScript. Unlike JavaScript, TypeScript provides great user readability meaning that code is much to review and understand. These are features that can help greatly when writing code.
Conclusion
TypeScript consists of countless groundbreaking and revolutionary features. From its type safety to error handling, TypeScript provides the developer with many helpful features. JavaScript developers also will take less time learning the language due to the fact that it is just enhanced JavaScript. TypeScript also provides excellent user-readability, making the process of reviewing code so much easier. When writing code, it is better to be “type-safe” than sorry, and this is exactly what TypeScript provides.