Will TypeScript replace JavaScript?

JavaScript is everywhere. JavaScript was developed by NetScape Communications during the early years of web. After the release of Mosaic browser NetScape realized the fact that web needs to be more dynamic. So they created JavaScript formerly known as LiveScript.

TypeScript is a superset of JavaScript which was developed and maintained by Microsoft. Microsoft developed TypeScript to overcome the shortcomings of JavaScript. The first version of TypeScript was released on October 1, 2012.

As the challenges in maintaining complicated JavaScript code became a hindrance to the developers Microsoft came up with the idea of TypeScript. TypeScript is meant to help the development of largescale applications. As we said earlier TypeScript is a superset of JavaScript the code we write in TypeScript gets transcompiled into JavaScript by the TypeScript compiler. TypeScript can be installed via NPM (Node Package Manager).

npm install -g typescript

Now let us see how we can write Hello World in JavaScript:

function sayHello(person) {

alert( “Hello, ” + person);

}

var user = “John Samuel”;

sayHello(user);

Let us save this file as HelloWorld.js. This is just for reference.

Now let’s write the same code using TypeScript:

function sayHello(person: string) {

alert( “Hello, ” + person);

}

var user = “John Samuel”;

sayHello(user);

Now save this file as HelloWorldTypeScript.ts. Now we need to compile this file.

tsc HelloWorldTypeScript.ts

After running the above command, we will get HelloWorldTypeScript.js file.

If you check the contents of this file, it will be same as the HelloWorld.js file we created first. So what is the difference? The difference is TypeScript offers a lot of powerful features that JavaScript doesn’t have. We have already seen one such feature. It is called type annotation. We have specified that the parameter to sayHello function as a String. If we pass any other type, the compiler will throw an error during compilation. This is an extremely useful feature while developing large scale applications.

Apart from the above mentioned one, TypeScript offers a lot of cool new features.

  • Inheritance
  • Overriding
  • Find unreachable code
  • String enumerations
  • Non-Nullable type
  • Control flow based type analysis
  • Read-only properties and index signatures
  • Shorthand ambient module declarations and wildcard matching in module names
  • Support private and protected constructors
  • Support abstract properties

Now that we have learned basic difference between JavaScript and TypeScript and how to transpile it, we understands the future of TypeScript is promising.