Why TypeScript

TypeScript is a free and open-source programming language developed and maintained by Microsoft. It is a superset of Javascript (ES6) and adds static typing.

Estimated reading time: 10 minutes

Let’s analyze the top reasons to use TypeScript

Adds the Types

Almost everyone faces the runtime exceptions which are caused because of the wrong data types.
Let’s look at the below code to understand what happens without types

function greeter(person) {
   return “Hello, “ + person;
}

The above code accepts any person argument, it may be a string, integer or boolean. But in the real world, we always want to pass a specific type.

Thanks to TypeScript, now we can specify the type of the person, we can pass.

function greeter(person: string) {
  return “Hello, “ + person;
}

Most Server side languages won’t face this problem as they are strongly typed. Strongly typed means, the compiler will throw an error if we try to store a different type. This is the reason, there were lot’s of IDE’s which have strong tooling support for Java/.NET, example: Eclipse shows the errors as you save Java/C# Code.

Now due to the added types, the IDES can show the error when we try to pass a different type than expected.

For example in VSCode editor, if we try to pass the integer instead of a string, the compiler will show the error after you save the typescript file.

Features from upcoming JavaScript versions (ES6 and beyond)

TypeScript is a superset of ES6, which essentially means we can simply use all of the ES6 features. We may even use some of the ES7 features.

We bring lot’s of robustness and eliminate the bugs present in the current version of Javascript.

For example we can simply use the classes from ES6

class Human {
   constructor() {
      console.log(“creating human”);
      this.name = “I am human”;
   }
}

Interfaces

Not just the ES6 Classes, we can now program to interfaces.

interface Person {
   name: string;
}
class Human {
   constructor(public name) {
     console.log(“creating human”);
     this.name = “I am human”;
   }
}
function greeter(person : Person) {
   return “Hello, “ + person.name;
}
var user = new Human(“Frank”);

Some advantages of Programming to Interfaces are:

– Loose coupling: Clients remained decoupled and unaware of actual implementation.
– Polymorphism makes it easy for the system to evolve.
– Speeds up the development, even though the actual class is not available, the client class can be developed

IntelliSence for the IDE’s

Most Server side languages have strong tooling support for Java/.NET, example: Eclipse shows the autocomplete/IntelliSense as you type Java/C# Code

Now due to the added types, the IDES can enable the features like autocomplete, check the below where as you type the greeter method, you will get the IntelliSense which shows the arguments along with the type

All these benefits are available at development time which helps reduce errors and deliver error free application.

TypeScript code is compiled into simple JavaScript. We can configure the output version of the Javascript (target it for ES3 or ES5 as per our convinience).

If you liked this, click the💚 below so other people will see this here on Medium.

To explore more awesome articles about NodeJS, ReactJS, AngualrJS and much more, please check out

My Website
http://whyjsnow.com/2016/11/javascript/why-angularjs1/

Facebook Group, WHYJSNOW
https://www.facebook.com/whyjsnow/

Linked IN
https://www.linkedin.com/pulse/why-angularjs1-basha-shaik

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.