What is Typescript and Why You Should Use it?

Last updated on May 04,2020 3.7K Views
A Data Science Enthusiast with in-hand skills in programming languages such as... A Data Science Enthusiast with in-hand skills in programming languages such as Java & Python.

What is Typescript and Why You Should Use it?

edureka.co

TypeScript is a strongly typed superset of JavaScript that compiles to plain JavaScript. You can use this language for application-scale JavaScript development. Also, it can be executed on any browser, any host, and any Operating System. In this article, we will understand what is TypeScript and why do we need to use it, in the following sequence:

Let’s begin.

What is TypeScript?

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It is pure object-oriented with classes, interfaces and statically typed programming languages like C# or Java. You will need a compiler to compile and generate the code in the JavaScript file. Basically, TypeScript is the ES6 version of JavaScript with some additional features.

A TypeScript code is written in a file with .ts extension and then compiled into JavaScript using the compiler. You can write the file in any code editor and the compiler needs to be installed on your platform. After the installation, the command tsc <filename>.ts compiles the TypeScript code into a plain JavaScript file.

Syntax:

var message:string = "Welcome to Edureka!"
console.log(message)

On compiling, it generates the following JavaScript code:

//Generated by typescript 1.8.10
var message = "Welcome to Edureka!";
console.log(message);

Now that you have understood what is TypeScript, let’s see why do we need to use it.

Why do we use TypeScript?

There are different reasons why a JavaScript developer should consider using TypeScript. Some of them include:

Now let’s move with our What is TypeScript article and have a look at some of the features.

Features of TypeScript

 

 

Now let’s see what are the different benefits of using TypeScript.

Benefits of Using TypeScript

Now that you know what is TypeScript and its features, let’s understand the different components of TypeScript.

Components of TypeScript

There are three different types of components in TypeScript which includes:

 

 

 

How to Install TypeScript?

There are two main ways to install TypeScript tools such as:

  1.  Via npm (Node.js Package Manager) command-line tool
    npm install -g typescript
  2. By installing TypeScript via Visual Studio.

If you use Visual Studio or VS Code IDE, the easiest way to add to Visual Studio or VS Code is to search and add a package or download from the TypeScript website. Also, you can download TypeScript Tools for Visual Studio.

 

You can click on download and install TypeScript in your system.

 

Working of TypeScript with Example

In JavaScript, we declare any variable in the following way:

var selectedValue=" ";

But in TypeScript, the declaration is done in the following way:

AccessModifier Property/Variable Name Colon(:) dataType

 

Example:

var name:string = "Daisy";
var empid:number = 1001;
console.log("name"+name)
console.log("employee id "+empid)

On compiling, it will generate following JavaScript code:

//Generated by typescript 1.8.10
var name = "Daisy";
var empid = 1001;
console.log("name" + name);
console.log("employee id: " + empid);

Output:

name:Daisy
employee id:1001

Difference Between TypeScript & JavaScript

TypeScriptJavaScript
 

 

  • TypeScript is an Object-Oriented language
  • It has a feature known as Static typing
  • TypeScript gives support for modules
  • It supports optional parameter function
 

 

  • JavaScript is a Scripting language
  • It does not have static typing
  • JavaScript does not support modules
  • It does not support optional parameter function

To know more about the differences, refer to the full-fledged article on TypeScript vs JavaScript.

With this, we have come to the end of this article. I hope you understood What is TypeScript. If you want to know the basics and fundamentals of TypeScript, You can check out the TypeScript Tutorial for better understanding.

Check out the Web Development Certification Training by Edureka. Web Development Certification Training will help you Learn how to create impressive websites using HTML5, CSS3, Twitter Bootstrap 3, jQuery and Google APIs and deploy it to Amazon Simple Storage Service(S3). 

Got a question for us? Please mention it in the comments section of “What is TypeScript?” and we will get back to you.

Upcoming Batches For Full Stack Web Development Course
Course NameDateDetails
Full Stack Web Development Course

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR A Comprehensive UI/UX Design Tutorial