Updated July 8, 2023
Definition of TypeScript Multiple Constructors
In TypeScript, we cannot define multiple constructors like other programming languages because it does not support multiple constructors. But we have some alternatives which can be used to define multiple constructors in TypeScript, but there is no direct way to direct multiple constructors as we do in Java, c++, and other object-oriented programming languages. To implement this, we have to give a common implementation for all constructors we have defined in a TypeScript class. In the coming section, we will discuss in more detail about implementation to make use of it while application development.
Syntax:
As discussed, we have to give a common implementation of all constructors in order to have multiple constructors support in TypeScript. Let’s see its syntax for a better understanding of its usage; see below;
public constructor(...args: any[]) {
// your logic will go here ..
}
In the above lines of code, as you can see, we are just using the ‘constructor’ keyword to define a constructor implementation. Inside this, we are taking arguments array. Let’s see one practice syntax for multiple constructor support in TypeScript. For a better understanding, see below;
e.g. :
public constructor(...args: any[]) {
if (args.length === your value) {
// logic for your called constructor goes here..
}
}
As you can see, we are checking the number of the argument here to check which constructor is being called. In the coming section, we will discuss more its internal working and how we can write and use the logic for this constructor to make our code efficient.
How to work with Multiple constructors?
As we already know that we have used constructors implementation to give support for multiple constructors in TypeScript. But there is no direct way to implement them easily. We have to use some alternative ways in TypeScript. We can define a number of constructors in our class, but we have to give one common implementation for all the constructors defined inside this constructor; we can write our own logic in TypeScript. In this section, we will discuss its internal working with a sample piece of code for beginners; let’s get started. See below;
constructors implementation: This is used to give implementation for all the constructors defined in the TypeScript class. We can individually check the argument, or else we can define constructors that accept any argument array, and then we can reduce the length of it. Let’s take a simple example for better understanding. See below;
e.g. :
class DemoClassTest {
public constructor(x : string, y:string);
public constructor(x : number);
public constructor(x : number, y:string, z:string);
public constructor(...myarray: any[]) {
if (myarray.length === 2) {
console.log('two argument constructor called here !!');
return;
}
if (myarray.length === 3) {
console.log('three argument constructor called here !!');
return;
}
if (myarray.length === 1) {
console.log('one argument constructor called here !!');
return;
}
}
}
let a = new DemoClassTest('hello', 'bye');
let b = new DemoClassTest(1);
let c = new DemoClassTest(100, 'str1', 'str2');
As you can see in the above example, we have defined so many constructors inside the class which accept a different parameter of a different type. We have defined one constructor which accepts an argument array; inside this, we are changing how many arguments are being passed while creating the instance for the class, then we can apply the logic we want to execute in TypeScript. Also, at last, we are creating a different object to invoke the constructor of a different type. first, we have created the object ‘a’, which accepts string and string. The second one is called ‘b’, which accepts only one parameter as a number type. The third parameter is ‘c’, which accepts the three-parameter number, string, and string according to the passing argument; the logic inside the implemented constructor will be executed.
Examples
In this example we are trying to implement multiple constructor support; this is a simple example for beginners to understand the concept of multiple constructors in Typescript in detail.
Code:
class DemoClassTest {
public constructor(x : string, y:string);
public constructor(x : number);
public constructor(x : number, y:string, z:string);
public constructor(...myarray: any[]) {
if (myarray.length === 2) {
console.log("arugument length is :: " + myarray.length)
console.log('two argument constructor called here !!');
return;
}
if (myarray.length === 3) {
console.log("arugument length is :: " + myarray.length)
console.log('three argument constructor called here !!');
return;
}
if (myarray.length === 1) {
console.log("arugument length is :: " + myarray.length)
console.log('one argument constructor called here !!');
return;
}
}
}
console.log("Example to show multiple constructor support in Typescript !!")
let result1 = new DemoClassTest('hello', 'bye');
let result2 = new DemoClassTest(1);
let result3 = new DemoClassTest(100, 'str1', 'str2');
console.log("Printing result here !!")
console.log( "result one is :::" + result1)
console.log("result two is :::" +result2)
console.log("result three is :::" +result3)
Output:
Rules and Regulations for multiple constructors
There are some rules that we need to follow while implementing multiple constructors in TypeScript, which are as follows see below;
1) First thing to keep in mind is that TypeScript does not support the implementation of multiple constructors directly. We have to use alternative ways to support multiple constructors.
2) If you have defined multiple constructors in the TypeScript class, then we have to define a common implementation of all the constructors that we have already discussed.
3) If we have a constructor which accepts the same number of the argument but a different type, then additionally, we have to apply the condition to check the type of passed argument as well.
4) if we do not provide an implementation of the constructor inside the multiple constructor TypeScript class, then it will throw a runtime exception.
5) Multiple constructors in Typescript, if not correctly implemented, may throw exceptions; we can implement any number of constructors inside our Typescript class.
Conclusion
TypeScript supports multiple constructors by providing the implementation of a constructor. There is no simple and direct way to implement this. In this article, we have already discussed the ways to support multiple constructors in TypeScript. This will make the application code efficient and reusable with some logic.
Recommended Articles
We hope that this EDUCBA information on “TypeScript Multiple Constructors” was beneficial to you. You can view EDUCBA’s recommended articles for more information.