I discovered TypeScript some years ago while trying to understand how Angular had changed between major versions. As you know, the changes between the first and the second version of this framework were huge, so I wanted to give it a try. Everything was written in TypeScript but like you, I didn’t know why. Let’s see which are the benefits of using this superset of JavaScript 🚀.
As you all know, JavaScript has no types, so it’s hard to control all the parameters and variables that we are using and validate them. It’s really easy to make mistakes on your code like forgetting to declare a variable, calling a non-existing function or passing as a parameter a variable that will break all our code.
So we can say that TypeScript is like JavaScript, but with types. It helps make our code easier to read and avoid errors that may become a debugging nightmare.
Let’s see an example of how the code changes from JavaScript to TypeScript:
class AlligatorsService {
public alligators = [];
public addAlligator(alligator) {
if (this.isValid(alligator)) {
alligators.push(alligator);
}
}
class AlligatorsService {
public alligators: Alligator[] = [];
public addAlligator(alligator: Alligator): void {
if (this.isValid(alligator)) {
alligators.push(alligator);
}
}
private isValid(alligator: Alligator): boolean {
return alligator.name;
}
}
Here, Alligator
is an interface that could be defined anywhere and imported into our file. The interface defines the shape for an object of type Alligator.
As you can see from the highlighted code, we’ve added a description of the parameters and the return types. It gives you more context and when the TypeScript compilator transforms your code, it’ll notify you about errors if using wrong types in the wrong places.
Another cool thing about TypeScript is that we can use the last JavaScript features on our code. Not all the modern browsers can understand our code if we use the last features and, usually, we need to use additional tools like Babel to make it possible. However, if we use TypeScript, the compiler will do all the hard work.
Since TypeScript is a package that we can install in our projects via npm, if we keep the version updated, we’ll automatically have all the new stuff available.
Since the compiler is transforming your code to JavaScript, you can use TypeScript on both sides, the client and the server. 🔥
Most modern IDEs will help you while coding. Since TypeScript is a typed language, the IDE will provide you with some code hinting.
In my case, I use Atom and it has a great package for TypeScript that will save you a ton of time.
Browser compatibility is the feature that I love the most. Forget about the problems with IE and the compatibility with your code, the compiler does some magic to transform your code and make it compatible with all modern browsers. In other words, by default the code that the TypeScript compiler spits out is ES5-compliant, and all modern browsers understand the ES5 flavor of JavaScript.
If you haven’t tried TypeScript yet, I strongly recommend you give it a try. As a JavaScript developer, it’ll provide you with some capabilities that will make your job easier. Less horrible debugging and less silly errors should end up saving you some precious time.
Check out our post on setting up a new TypeScript project for some guidance on getting start with a new TypeScript project.
If you were already a TypeScript developer, I hope this post may have cleared a bit why you should keep using it for your next projects.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!