Javatpoint Logo
Javatpoint Logo

TypeScript Classes

In object-oriented programming languages like Java, classes are the fundamental entities which are used to create reusable components. It is a group of objects which have common properties. A class is a template or blueprint for creating objects. It is a logical entity.

A class can contain:

  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface

Since TypeScript is an Object-Oriented JavaScript language, so it supports object-oriented programming features like classes, interfaces, etc. JavaScript ES5 or earlier version does not support class. They support classes from ES6 and later version. TypeScript has built-in support to use classes because it is based on ES6 version of JavaSript. The developers are allowed to use class-based object-oriented programming today, and compile them into JavaScript that works across all major browsers and platforms.

Syntax to declare a class:

class keyword is used declare a class in TypeScript. The syntax to create a class is given below:


The TypeScript compiler will convert the above class to the following JavaScript code:

Creating an object of class

A class creates an object by using the new keyword followed by the class name. The new keyword allocates memory for object creation at runtime. All objects get memory in heap memory area. The syntax of creating an object is given below.



Object Initialization

Object initialization means storing of data into the object. There are three ways to initialize an object. These are:

1. By reference variable


2. By method

A method is similar to a function which is used to expose the behavior of an object.

Advantage of Method

  • Code Reusability
  • Code Optimization


3. By Constructor

A constructor is used to initialized an object. In TypeScript, the constructor method is always defined with the name "constructor." In the constructor, the member of the class can be accessed by using this keyword.

Note: There is not necessary to always have a constructor in the class.


Example with constructor, method and object:


Reading attribute value of Student as: 101
Reading attribute value of Student as: Virat Kohli
Function displays Student ID is: 101
Function displays Student ID is: Virat Kohli

Example without constructor:


Student ID: 101
Student Name: Virat Kohli

Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA