Lesson 48 of 60 – Constructors
80%

Constructors in TypeScript

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.

Note: A constructor is written using the constructor() keyword. It runs automatically when we create an object using new.

1. What is a Constructor?

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");
    }
}

2. constructor() Keyword

TypeScript uses the constructor() keyword to define a constructor.

class Student {
    constructor() {
        console.log("Constructor called");
    }
}

3. Constructor Runs Automatically

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();

4. Creating an Object with new

The new keyword creates an object and automatically calls the constructor.

const student = new Student();

5. Constructor with a Property

A constructor can initialize class properties.

class Student {
    name: string;

    constructor() {
        this.name = "Rahul";
    }
}

6. Using this Inside Constructor

The this keyword refers to the current object.

class Student {
    name: string;

    constructor() {
        this.name = "Amit";
    }
}

7. Constructor Parameters

Constructors can receive parameters when an object is created.

class Student {
    name: string;

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

8. Passing Constructor Arguments

Values can be passed while creating the object.

const student = new Student("Rahul");

The value "Rahul" is received by the constructor.

9. Multiple Constructor Parameters

A constructor can have multiple parameters.

class Student {
    name: string;
    age: number;

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

10. Creating Object with Multiple Values

const student = new Student("Priya", 20);

console.log(student.name);
console.log(student.age);

The constructor initializes both properties.

11. Constructor Parameter Types

Constructor parameters can have TypeScript types.

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

12. Initializing Number Properties

class Product {
    price: number;

    constructor(price: number) {
        this.price = price;
    }
}

const product = new Product(500);

13. Initializing Boolean Properties

class User {
    active: boolean;

    constructor(active: boolean) {
        this.active = active;
    }
}

const user = new User(true);

14. Constructor with Default Values

Constructor parameters can have default values.

class Student {
    name: string;

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

15. Using the Default Constructor Value

const student = new Student();

console.log(student.name);

Since no argument is supplied, the default value is used.

16. Constructor and Methods

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);
    }
}

17. Calling a Method After Constructor

const student = new Student("Anita");

student.showName();

The constructor first initializes the object, and then its methods can be used.

18. Constructor Initialization

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;
    }
}

19. Constructor Parameter Property

TypeScript provides a shorter way to create and initialize properties directly through constructor parameters.

class Student {
    constructor(public name: string) {
    }
}

20. public Parameter Property

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);

21. private Parameter Property

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.

22. readonly Constructor Property

A constructor parameter can also create a readonly property.

class Student {
    constructor(readonly id: number) {
    }
}

The value cannot be reassigned after initialization.

23. Combining Access Modifiers

class Student {
    constructor(
        public name: string,
        private age: number,
        readonly id: number
    ) {
    }
}

Different constructor parameters can have different access modifiers.

24. Constructor in Inheritance

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);
    }
}

25. Using super() in Constructor

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();
    }
}

26. Constructor Without Parameters

A constructor does not always need parameters.

class Message {
    constructor() {
        console.log("Welcome!");
    }
}

const message = new Message();

27. Constructor with Multiple Properties

class Employee {
    constructor(
        public name: string,
        public department: string,
        public salary: number
    ) {
    }
}

const employee = new Employee(
    "Amit",
    "IT",
    30000
);

28. Practical Student Example

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();

29. Common Constructor Mistakes

  • Writing the constructor name incorrectly.
  • Forgetting to initialize required properties.
  • Passing the wrong number of arguments.
  • Using incorrect parameter types.
  • Forgetting super() in a derived class.
  • Trying to access private properties outside the class.

30. Complete Constructor Example

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.

📌 Key Points

  • A constructor is a special method of a class.
  • It is defined using constructor().
  • It runs automatically when an object is created.
  • The new keyword creates the object.
  • Constructors can accept parameters.
  • Parameters can have TypeScript types.
  • Constructors are commonly used to initialize properties.
  • TypeScript supports parameter properties.
  • public, private, and readonly can be used with constructor parameters.
  • Derived class constructors can use super().

🧠 Quick Quiz

Question: Which keyword is used to define a constructor in TypeScript?