TypeScript Classes

What Are Classes in TypeScript?

Classes in TypeScript are fundamental to object-oriented programming. They allow developers to create reusable blueprints for objects, which encapsulate both data and behavior. This makes code more structured, organized, and easier to maintain.

In TypeScript, a class can include fields (properties), methods (functions), constructors, and even nested classes. Fields represent the state of an object, while methods define the actions an object can perform. Constructors are special methods automatically called when an object is created, and nested classes offer further organization.

Example of a TypeScript Class

Importance of Classes in Object-Oriented Programming

Classes are crucial in object-oriented programming because they define the blueprint for creating objects, which represent entities or concepts in a program. By using classes, developers can:

  • Encapsulate Data and Behavior: Combine related data and functions into a single unit.
  • Promote Code Reuse: Reuse code by creating multiple objects from the same class.
  • Enhance Organization: Keep code organized and easier to maintain.
  • Enable Inheritance: Create subclasses that inherit properties and methods from other classes, reducing redundancy.

Constructor Functions in TypeScript

Constructor functions are key in object-oriented programming within TypeScript. They initialize the object with specific values or perform necessary setup tasks. Defined with the constructor keyword, these functions can take parameters to set the initial values of an object's properties.

Example of a Constructor Function

class Book {
    title: string;
    author: string;
    year: number;
    genre: string;

    constructor(title: string, author: string, year: number, genre: string = 'Unknown') {
        this.title = title;
        this.author = author;
        this.year = year;
        this.genre = genre;
    }
}

let myBook = new Book('1984', 'George Orwell', 1949);
console.log(myBook);

The class Keyword in TypeScript

The class keyword in TypeScript is used to declare a class. It serves as a template for creating objects, defining their structure, properties, and behaviors. The class keyword supports creating reusable components, inheritance, and organizing code in an object-oriented way.

Defining a Class Constructor

Class constructors are special functions that are automatically called when a new instance of a class is created. Constructors initialize the class properties and ensure that objects are properly set up before use.

Example of a Class Constructor

class Car {
    make: string;
    model: string;
    year: number;

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

let myCar = new Car('Toyota', 'Camry', 2020);

Abstract Classes in TypeScript

An abstract class in TypeScript cannot be instantiated directly but serves as a base for other classes. It can define methods, properties, and accessors without implementation, which subclasses must then implement. This promotes code consistency and reuse.

Example of an Abstract Class

abstract class Animal {
    abstract makeSound(): void;

    move(): void {
        console.log('Roaming the earth...');
    }
}

class Dog extends Animal {
    makeSound(): void {
        console.log('Bark');
    }
}

const myDog = new Dog();
myDog.makeSound(); // Output: Bark
myDog.move(); // Output: Roaming the earth...

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate