Constructor in TypeScript

Introduction

TypeScript is an open-source programming language designed and developed by Microsoft. TypeScript is a strict syntactical superset of JavaScript that adds optional static typing to the language.

Constructors are a key concept in object-oriented programming. A constructor is a special type of method that is responsible for creating and initializing an object when it is instantiated. Constructors are used to set the initial values of an object’s properties and to perform any other initialization tasks that are required before the object can be used.

In TypeScript, constructors are declared using the `constructor` keyword. The constructor is a method that is automatically called when an object of a class is created. In this article, we will explore constructors in TypeScript and their usage.

Syntax of Constructor in TypeScript

The syntax of a constructor in TypeScript is as follows:

“`
class ClassName {
constructor(param: type) {
// code to be executed
}
}
“`

In the above syntax, the `ClassName` is the name of the class and `param` is an optional parameter that can be passed to the constructor. The body of the constructor is enclosed in curly braces `{}`.

Usage of Constructor in TypeScript

Constructors are used to initialize the values of the properties of an object and to perform any other initialization tasks that are required before the object can be used.

Let’s consider the following example:

“`
class Person {
firstName: string;
lastName: string;

constructor(fname: string, lname: string) {
this.firstName = fname;
this.lastName = lname;
}

greeting() {
console.log(“Hello ” + this.firstName + ” ” + this.lastName);
}
}

let person1 = new Person(“John”, “Doe”);
person1.greeting(); // output: Hello John Doe
“`

In the above example, we have a class `Person` with two properties `firstName` and `lastName`. The constructor of the class takes two parameters for initializing these properties. The `greeting` method of the class is used to display a message on the console.

When we create an object `person1` of the class `Person` using the `new` keyword, the constructor of the class is called automatically with the passed parameters. The `greeting` method is then called to display a message on the console.

Multiple Constructors in TypeScript

In TypeScript, we can define multiple constructors for a class. This is useful when we want to provide flexibility to the user to create an object with different initial values.

Consider the following example:

“`
class Person {
firstName: string;
lastName: string;

constructor(fname: string, lname: string) {
this.firstName = fname;
this.lastName = lname;
}

greeting() {
console.log(“Hello ” + this.firstName + ” ” + this.lastName);
}

constructor() {
this.firstName = “Unknown”;
this.lastName = “Unknown”;
}
}

let person2 = new Person();
let person3 = new Person(“Jane”, “Doe”);
person2.greeting(); // output: Hello Unknown Unknown
person3.greeting(); // output: Hello Jane Doe
“`

In the above example, we have defined two constructors for the `Person` class. The first constructor takes two parameters `fname` and `lname` for initializing the properties of the object. The second constructor initializes the properties to unknown values.

When we create an object `person2` with no parameters, the second constructor of the class is called automatically. When we create an object `person3` with two parameters, the first constructor of the class is called automatically.

FAQs

1. What is a constructor in TypeScript?

A constructor is a special type of method that is responsible for creating and initializing an object when it is instantiated. Constructors are used to set the initial values of an object’s properties and to perform any other initialization tasks that are required before the object can be used.

2. How to declare a constructor in TypeScript?

In TypeScript, constructors are declared using the `constructor` keyword. The constructor is a method that is automatically called when an object of a class is created.

3. What is the use of a constructor in TypeScript?

Constructors are used to initialize the values of the properties of an object and to perform any other initialization tasks that are required before the object can be used.

4. Can we define multiple constructors in TypeScript?

Yes, in TypeScript, we can define multiple constructors for a class. This is useful when we want to provide flexibility to the user to create an object with different initial values.

5. How to call a constructor in TypeScript?

When we create an object of a class using the `new` keyword, the constructor of the class is called automatically. We can pass parameters to the constructor to initialize the properties of the object.

Similar Posts