Tutorial

Benefits of Using TypeScript

Published on January 14, 2019
author

Miquel Arranz

Benefits of Using TypeScript

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 🚀.

Typing

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:

alligators-service.js
class AlligatorsService {
  public alligators = [];

  public addAlligator(alligator) {
    if (this.isValid(alligator)) {
      alligators.push(alligator);
    }
  }

alligators-service.ts
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.

Supports the Latest JavaScript Features

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. 🔥

IDE Support

Most modern IDEs will help you while coding. Since TypeScript is a typed language, the IDE will provide you with some code hinting.

Code Hinting TypeScript with Atom.

In my case, I use Atom and it has a great package for TypeScript that will save you a ton of time.

Browser compatibility

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.

Next steps

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.

Learn more about our products

About the authors
Default avatar
Miquel Arranz

author

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.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.