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 can’t 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 give an example here really 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();
So, if you see here to initialize the user property (variable) we need to write extra statement here after creating an object of the class,
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 initialize 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 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 cannot have multiple constructors in single class.
Constructor function with optional Parameters
Sometimes you may need variable optional those does not need to be provide a default value depending on the requirements logic that you’re working on.
So let me show you how to define an optional function parameter here in typescript, it is really easy you just need 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 can’t 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 you’re working on.