
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.
Table of Contents
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class User { name: string; profile() { console.log(`User name: ${this.name}`); } } let user: User = new User(); user.name = 'iTech Empires'; user.profile(); |

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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.

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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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.