Search Tutorials


Understanding the need for TypeScript file in Angular2 applications | JavaInUse

Understanding the need for TypeScript file in Angular2 applications.

Overview

TypeScript is a free and open-source programming language developed and maintained by Microsoft.
TypeScript is an open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, and adds optional static typing to the language. Anders Hejlsberg, lead architect of C# and creator of Delphi and Turbo Pascal, has worked on the development of TypeScript. TypeScript may be used to develop JavaScript applications for client-side or server-side (Node.js) execution. On visiting the TypeScript website https://www.typescriptlang.org/ we see the following screen.

ang2_4_1
Here they have given the definition of TypeScript as follows-
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.

ang2_4_2
This compiler which converts typescript file to Javascript is called a Transpiler. So if this is the case why can't users directly write in JavaScript instead of TypeScript which also compiles to JavaScript.

Need for TypeScript-


JavaScript rules in Web development. Its the most popular language for developing web application UI. For may application developers having exposure in languages like Java and C#, creating the front end of a Web application in JavaScript is a very cumbersome process. For example if the user wants to create a class Employee in JavaScript. There is no class keyword in JavaScript so the code will be as follows-
<html>
<head>
</head>
<body>
<script>
function Employee()
{
	this.name="";
	this.id="";
	this.Validate=function(){
		alert("Validate");
		}
}
</script>
</body>
</html>
Same can be written using TypeScript as follows-
class Employee{
	public name : string = "";
	public id : string = "";
	Validate(){
	alert("validate");
	}
}

This Customer.ts will compile to the above JavaScript code.

So TypeScript provides the following advantages over JavaScript-
  • Structure the code-
    There were many different coding styles for JavaScript. This leads to unstructured code. With TypeScript we create structured code.
  • Use object-oriented programming paradigms and techniques-
    There is lack of object-oriented design paradigms and techniques in JavaScript. This is not the case in TypeScript. It makes use of Objected Oriented features like Polymorphism, Inheritance etc.
  • Standard Coding guidelines-
    There is no Type checking in JavaScript. The code style needs to be defined. Hard to enforce style guide. TypeScript overcomes this issue with features like Code Analysis and Navigation, Documentation, Intellisense etc.
 

See Also

Removing the # from the AngularJS URL Use Google reCaptcha with AngularJS Use AngularJS NgCloak directives Example Angular 2 Main Menu Angular 2 interview questions