A constructor is a special method inside a TypeScript class that is automatically called when an object is created. Constructors are mainly used to initialize object properties.
constructor() keyword.
It runs automatically when we create an object using new.
A constructor is a special method of a class used to initialize an object when it is created.
class Student {
constructor() {
console.log("Student object created");
}
}
TypeScript uses the constructor() keyword to define
a constructor.
class Student {
constructor() {
console.log("Constructor called");
}
}
We do not manually call the constructor. It runs automatically when an object is created.
class Student {
constructor() {
console.log("Hello Student");
}
}
const s1 = new Student();
The new keyword creates an object and automatically
calls the constructor.
const student = new Student();
A constructor can initialize class properties.
class Student {
name: string;
constructor() {
this.name = "Rahul";
}
}
The this keyword refers to the current object.
class Student {
name: string;
constructor() {
this.name = "Amit";
}
}
Constructors can receive parameters when an object is created.
class Student {
name: string;
constructor(name: string) {
this.name = name;
}
}
Values can be passed while creating the object.
const student = new Student("Rahul");
The value "Rahul" is received by the constructor.
A constructor can have multiple parameters.
class Student {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
const student = new Student("Priya", 20);
console.log(student.name);
console.log(student.age);
The constructor initializes both properties.
Constructor parameters can have TypeScript types.
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
class Product {
price: number;
constructor(price: number) {
this.price = price;
}
}
const product = new Product(500);
class User {
active: boolean;
constructor(active: boolean) {
this.active = active;
}
}
const user = new User(true);
Constructor parameters can have default values.
class Student {
name: string;
constructor(name: string = "Unknown") {
this.name = name;
}
}
const student = new Student();
console.log(student.name);
Since no argument is supplied, the default value is used.
A class can contain both a constructor and other methods.
class Student {
name: string;
constructor(name: string) {
this.name = name;
}
showName(): void {
console.log(this.name);
}
}
const student = new Student("Anita");
student.showName();
The constructor first initializes the object, and then its methods can be used.
Constructors are useful when an object needs initial data.
class Employee {
name: string;
salary: number;
constructor(name: string, salary: number) {
this.name = name;
this.salary = salary;
}
}
TypeScript provides a shorter way to create and initialize properties directly through constructor parameters.
class Student {
constructor(public name: string) {
}
}
Using public in a constructor parameter creates
a public property automatically.
class Student {
constructor(public name: string) {
}
}
const student = new Student("Ravi");
console.log(student.name);
A constructor parameter can use private to create
a private property.
class Account {
constructor(private balance: number) {
}
}
The property can be accessed only inside the class.
A constructor parameter can also create a
readonly property.
class Student {
constructor(readonly id: number) {
}
}
The value cannot be reassigned after initialization.
class Student {
constructor(
public name: string,
private age: number,
readonly id: number
) {
}
}
Different constructor parameters can have different access modifiers.
When a class extends another class, its constructor can call
the parent constructor using super().
class Person {
constructor(public name: string) {
}
}
class Student extends Person {
constructor(name: string) {
super(name);
}
}
The super() call initializes the parent class.
In a derived class constructor, it must be called before
accessing this.
class Child extends Parent {
constructor() {
super();
}
}
A constructor does not always need parameters.
class Message {
constructor() {
console.log("Welcome!");
}
}
const message = new Message();
class Employee {
constructor(
public name: string,
public department: string,
public salary: number
) {
}
}
const employee = new Employee(
"Amit",
"IT",
30000
);
class Student {
constructor(
public id: number,
public name: string,
public course: string
) {
}
showDetails(): void {
console.log(this.id);
console.log(this.name);
console.log(this.course);
}
}
const student = new Student(
101,
"Rahul",
"TypeScript"
);
student.showDetails();
super() in a derived class.class Student {
constructor(
public id: number,
public name: string,
public age: number,
public course: string
) {
}
showDetails(): void {
console.log("ID:", this.id);
console.log("Name:", this.name);
console.log("Age:", this.age);
console.log("Course:", this.course);
}
}
const student = new Student(
101,
"Priya",
20,
"React Native"
);
student.showDetails();
Here, the constructor receives four values and automatically initializes the four properties of the object.
constructor().new keyword creates the object.public, private, and readonly can be used with constructor parameters.super().Question: Which keyword is used to define a constructor in TypeScript?