TS Classes

Classes in TypeScript provide a blueprint for creating objects with properties and methods. TypeScript enhances classes with strong typing, access modifiers, and other features.

Key Topics

Class Basics

A basic class defines properties and methods for creating objects. Properties are declared and initialized within the class.

class Person {
    name: string;
    age: number;

    greet(): string {
        return `Hello, my name is ${this.name}.`;
    }
}

const person = new Person();
person.name = "Alice";
person.age = 25;
console.log(person.greet()); // Hello, my name is Alice.

Output

Hello, my name is Alice.

Explanation: The Person class defines properties name and age with a method greet. Objects are created using the new keyword.

Constructors

Constructors are special methods used to initialize objects when they are created. They can accept parameters to set property values.

class Animal {
    species: string;

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

    describe(): string {
        return `This is a ${this.species}.`;
    }
}

const animal = new Animal("Dog");
console.log(animal.describe()); // This is a Dog.

Output

This is a Dog.

Explanation: The Animal class constructor accepts a parameter species, which initializes the property when an object is created.

Access Modifiers

Access modifiers control the visibility of class members. The three types are:

  • public: Accessible from anywhere.
  • private: Accessible only within the class.
  • protected: Accessible within the class and its subclasses.
class Vehicle {
    private brand: string;

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

    getBrand(): string {
        return this.brand;
    }
}

const vehicle = new Vehicle("Toyota");
console.log(vehicle.getBrand()); // Toyota

Output

Toyota

Explanation: The brand property is private, so it cannot be accessed directly. The getBrand method provides controlled access.

Inheritance

Inheritance allows one class to extend another, inheriting its properties and methods.

class Shape {
    name: string;

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

    describe(): string {
        return `This is a ${this.name}.`;
    }
}

class Circle extends Shape {
    radius: number;

    constructor(radius: number) {
        super("Circle");
        this.radius = radius;
    }

    calculateArea(): number {
        return Math.PI * this.radius * this.radius;
    }
}

const circle = new Circle(5);
console.log(circle.describe()); // This is a Circle.
console.log(circle.calculateArea()); // 78.53981633974483

Output

This is a Circle.

78.53981633974483

Explanation: The Circle class extends Shape, inheriting its properties and methods. It also adds its own properties and methods.

Key Takeaways

  • Class Basics: Define properties and methods for objects.
  • Constructors: Initialize class properties during object creation.
  • Access Modifiers: Control visibility of class members.
  • Inheritance: Share functionality between classes using extends.