The Advantages of TypeScript

Image for The Advantages of TypeScript

TypeScript is a superset of the JavaScript programming language that evolved as an alternative to coding in pure JavaScript, giving developers a tool to help them write faster, cleaner JavaScript.

JavaScript is the foundation for nearly all interactive front-end programming, but it has a tendency to get verbose, tangled (“spaghetti code”), and repetitive in large amounts. Enter TypeScript, a typed, backwards-compatible version of JavaScript that compiles to pure JavaScript but makes writing that foundation easier—and makes projects sturdier from the start.

Join Upwork

 

Here’s a look at some of the key features and unique advantages of TypeScript.

TypeScript writes clean, scalable JavaScript.

Writing clean, object-oriented code in JavaScript can be a bit difficult for anyone, even those with extensive experience in object oriented programming. Naturally, the complexity of the code increases as the size of the project grows. This is where Typescript wins over JavaScript—you can very easily write pure object-oriented code in Typescript with little knowledge. Learn more about TypeScript and the similar JavaScript tool, CoffeeScript, in this article.

In addition to this, TypeScript

  • Is purely object-oriented programming
  • Can be used for client-side and server-side development alike
  • Offers a “compiler” that can convert to JavaScript-equivalent code
  • Has an API for DOM manipulation
  • Has a namespace concept by defining a “Module”

Here are a few key features of TypeScript.

1. Classes

TypeScript allows development by class, similar to Java, C++ or C#. The following is a valid class in TypeScript file with .ts extension:

Class Employee  {
            private firstName: string;
            private lastName: string;
            private email: string;
            private address: string;
}.

The above is a valid TypeScript class. Like Java, C++, or C#, TypeScript also has a support for “Constructor.” For example, the below code has declared a constructor:

Constructor(firstName:string, lastName:string, email:string, address:string){
This.firstName = firstName;
            This.lastName = lastName;
            This.address = address;
}

2. INTERFACES

Like in Java or C#, in TypeScript you can also declare an interface that can be “implemented.” The following code below defines an “interface”:

interface Vehicle {
color: string;
make: string;
}
class Car implements Vehicle {
color: string;
make: string;
constructor(color: string, make:string) {
    this.color = color;
    this.make = make;
}
}

Remember one thing about using interfaces in TypeScript: There is no equivalent JavaScript code generated. It’s only intended to be used for assuring correct type at the time of development.

3. Generics

The advantage of using “Generics” is that you can tell the compiler up front what the type is going to be. This will make program run a little bit faster, as “Object Type” has already been identified during compile time. Take a look at the following code for how to use a generic:

function makeCar(car: T) {
   return car;
}
let v = new Car("Blue", "Ford");
let mycar = makeCar(v);
alert("my car is  " + mycar.color + "  " + mycar.make);

4. DOM MANIPULATION

You can also use TypeScript to manipulate the DOM. The following code shows how a button can be added to the DOM:

let submit = document.createElement('button');
submit.textContent = "Submit";
document.body.appendChild(submit);

With all this, it’s easy to see how TypeScript streamlines JavaScript development by adding object oriented concepts like class, interface, and module statements to JavaScript so that highly clean and modular JavaScript code can be developed. The other benefit is that your code base can be easily maintained without much difficulty.

Typescript is gaining more popularity with the emergence of the Angular 2 framework, which currently supports ‘TypeScript’ as its favorite language.

This story was submitted by freelancer Unni Mana and does not constitute the views or opinions of Upwork.

Post a job on Upwork. It's free!
Unni Mana

by

Unni Mana is a Certified Java and Android developer with more than 12 years of professional experience in designing and developing web and mobile applications… more