TypeScript is an open-source programming language that is a superset of JavaScript. One of the key features of TypeScript is its support for object-oriented programming (OOP) concepts like classes, interfaces, and inheritance. In this tutorial, we will focus on implementing classes in TypeScript.


What is a class in TypeScript?

In TypeScript, a class is a blueprint for creating objects that share similar characteristics. It defines a set of properties and methods that the objects created from it will have.


Creating a class in TypeScript

To create a class in TypeScript, you use the class keyword followed by the name of the class, as shown in the example below:

class Car {
    // properties
    model: string;
    year: number;
    price: number;

    // constructor
    constructor(model: string, year: number, price: number) {
        this.model = model;
        this.year = year;
        this.price = price;
    }

    // methods
    getPrice(): string {
        return `$${this.price}`;
    }

    getModel(): string {
        return this.model;
    }
}

In the above example, we defined a Car class with three properties: model, year, and price. We also defined a constructor method that takes in three arguments and assigns their values to the properties. Finally, we defined two methods: getPrice() and getModel().


Instantiating a class

Once you have defined a class, you can create instances of that class using the new keyword, as shown in the example below:

const myCar = new Car("Honda Civic", 2022, 20000);

In the above example, we created an instance of the Car class and assigned it to a variable named myCar. We passed in three arguments to the constructor to set the initial values of the model, year, and price properties.


Accessing properties and methods

To access the properties and methods of an instance of a class, you use the dot notation, as shown in the example below:

console.log(myCar.getModel()); // Output: Honda Civic
console.log(myCar.getPrice()); // Output: $20000

In the above example, we accessed the getModel() and getPrice() methods of the myCar instance of the Car class and logged their output to the console.


Inheritance

One of the powerful features of OOP is inheritance, which allows you to create a new class based on an existing class. To create a subclass that inherits from a parent class in TypeScript, you use the extends keyword, as shown in the example below:

class ElectricCar extends Car {
    batteryCapacity: number;

    constructor(model: string, year: number, price: number, batteryCapacity: number) {
        super(model, year, price);
        this.batteryCapacity = batteryCapacity;
    }

    getBatteryCapacity(): number {
        return this.batteryCapacity;
    }
}


In the above example, we defined a new class ElectricCar that extends the Car class. The ElectricCar class has a new property batteryCapacity and a new method getBatteryCapacity(). The constructor method of the ElectricCar class also calls the super() method to invoke the constructor of the parent Car class and set the model, year, and price properties.


Conclusion

In this tutorial, we learned how to implement classes in TypeScript, create instances of a class, access properties and methods, and use inheritance to create subclasses. Classes are an essential part of TypeScript and allow you to write modular and maintainable code by encapsulating related properties and methods into a single entity. Understanding how to create and use classes in TypeScript is an essential skill for building complex applications that follow good OOP principles.

In addition to what we've covered in this tutorial, there are many other advanced features of classes in TypeScript, such as abstract classes, access modifiers, and static properties and methods. As you continue to develop your TypeScript skills, be sure to explore these features to take your programming to the next level.