Categories: TypeScript

What is constructor function in Typescript?

If you see in last tutorial What is object and How to Declare object of a class in TypeScript? I have created a simple class called User and we have profile() function in it along with the constructor() so in this tutorial I want you to give clear understanding on what is constructor function in Typescript and how constructors works and what is the specific use of constructor in typescript classes.

What is constructor?

In object oriented programming language there is a concept called constructor for classes, so every class can have constructor.

So constructor is basically a method which called or executed when create and instance or object of that particular class, constructor is reserved keyword in typescript meaning you cant have your custom function with the same name it is only used for creating constructor for the given class.

Similarly if you see here in this tutorial What is object and How to Declare object of a class in TypeScript? we had a User class along with constructor function.

What is need of using constructor?

To demonstrate needs of using constructor let me given an example here real quick, first I will create a class without constructor

Simple User class without constructor function

class User {
  name: string;

  profile() {
    console.log(`User name: ${this.name}`);
  }
}

let user: User = new User();

user.name = 'iTech Empires';

user.profile();
Typescript class without constructor function

So if you see here to initialize the user property (variable) we needs to write extra statement here after creating an object of the class,

So imagine in this example we just had one variable assume that we have number of properties those needs to be assign? how code will look it is going to be repetitive right.

That is why constructor function comes into place, let’s next the example of using constructor along with the same class.

Example of using constructor

I am going to write same User class here with addition of constructor function and you can see how we can initialised variables while creating the object of the class.

class User {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  profile() {
    console.log(`User name: ${this.name}`);
  }
}

let user: User = new User('itech Epires');

user.profile();

Now look at the above code and see how are are passing an argument to the class name which is being assigned to the constructor function and next is constructor functions job to assigned value to the defined variables according to the definition of the constructor function.

Typescript Class using constructor function

In TypeScript we can not have multiple constructors in single class.

Constructor function with optional Parameters

Sometime you may need variable optional those does not needs to be provide a default values depending on the requirements logic that your working on.

So let me show you hot to define an optional function parameters here in typescript, it is really easy you just needs to pass and question mark sign next to the parameter and that makes a variable optional name as showing below:

class User {
  name: string;

  constructor(name?: string) {
    this.name = name;
  }

  profile() {
    console.log(`User name: ${this.name}`);
  }
}

let user: User = new User();

user.profile();

Remember one important note, optional parameters should be the last in the parameter list because you cant have required parameter next to optional but you can have optional parameter next to required it is rule defined in Typescript and also lot of other object oriented programming languages.

Here is example of using Combination of both required and optional parameters within the same function:

class User {
  name: string;

  constructor(name: string, address?: string) {
    this.name = name;
  }

  profile() {
    console.log(`User name: ${this.name}`);
  }
}

let user: User = new User('Yogesh');

user.profile();

So you can see here is address parameter is optional so you don’t have to pass argument for the optional parameter while creating object of class and yes you can if you want to it’s all up to you and the project your working on.

Yogesh Koli

Software engineer & Blogger live in India, has 8+ years of experience working with the Front-end and Back-end Web Application Development.

Recent Posts

Complete guide of using Laravel 6 Eloquent Subquery Enhancements

Learn How to use laravel frameworks new improved feature called Eloquent Subquery and get example…

1 year ago

3 Useful examples of using Array Map function in PHP – Best Practices

Learn how to use php array map function with easy and essential tutorial to modify…

1 year ago

Working with PHP Array Filter Function – Best Practices

Learn how to use php array filter function with easy and essential tutorial to filter…

1 year ago

How to add Access Modifiers with Constructor Parameters in TypeScript

Want to know how to refactor your Typescript class, Learn here utilising Typescript of the…

1 year ago

What is Access Modifiers and how to use Access Modifiers in TypeScript ?

What is Access Modifiers in typescript, how to use Access Modifiers, when to use them,…

1 year ago

Top 10 Super Useful Packages to Improve Laravel applications in 2019

This tutorial provide ultimate list of package those are top 10 on packagist and super…

1 year ago