Constructors in TypeScript

constructor is a special method within a class that is automatically invoked when we create an instance of that class. Its primary purpose is to initialize the properties of the current instance. In TypeScript, constructors allow us to set up the initial state of an object.

Example:

javascript
class Person {
    constructor(public name: string, public age: number) {
        // Initialize properties
    }
}

const john = new Person('Uday', 20);
console.log(`Name: ${john.name}, Age: ${john.age}`);

Output:

[LOG]: "Name: Uday, Age: 20" 

Explanation:

  • We define a Person class with two properties: name and age.
  • The constructor takes two parameters (name and age) and assigns their values to the corresponding properties.
javascript
// converted javascript code
var Student = /** @class */ (function () {
    function Student(code, name) {
        this.studName = name;
        this.studCode = code;
    }
    Student.prototype.getGrade = function () {
        return "A+";
    };
    return Student;
}());

TypeScript class

TypeScript classes play a crucial role in creating robust and maintainable code. In this article, we’ll learn TypeScript classes, explore their features, and learn how to optimize them for better search engine ranking. Typescript is an open-source programming language built over Javascript, also known as the Superset of Javascript. Typescript has more features when compared to Javascript. It supports Object-oriented programming features like classes, Interfaces, Polymorphism, etc. 

Syntax to declare a class: 

class class_name{
field;
method;
}

Similar Reads

What Are Classes?

In TypeScript, classes are blueprints for creating objects. They encapsulate data (properties) and behavior (methods) into a single unit. By defining classes, you can organize your code, promote reusability, and enhance readability....

Key Components of TypeScript Classes

Methods: Functions defined within a class that perform specific actions.Constructors: Special methods called when an object is created from a class. Constructors initialize class properties.Properties: Variables associated with a class instance.Inheritance: The ability to create new classes based on existing ones, allowing code reuse and specialization....

Access Modifiers (public, private, and protected)

public: Properties and methods are accessible from outside the class.private: Restricts access to within the class itself.protected: Allows access within the class and its subclasses....

Constructors in TypeScript

A constructor is a special method within a class that is automatically invoked when we create an instance of that class. Its primary purpose is to initialize the properties of the current instance. In TypeScript, constructors allow us to set up the initial state of an object....

Objects in TypeScript

Objects An object is an instance of class which contains set of key value pairs. It’s value may be scalar values or functions or even array of other objects....