Lesson Overview

Greetings! Today, we're revisiting TypeScript classes, the core building block of Object-Oriented Programming (OOP) in TypeScript. Through hands-on examples, we'll explore the fundamental concepts of TypeScript classes, including their structure, properties, and methods, while highlighting TypeScript's powerful type system.

TypeScript Classes Refresher

Let's begin with a refresher on TypeScript classes. Essential to OOP, TypeScript classes bundle relevant data and functions into compact units called objects. Consider a video game character, which is a typical example of a class instance, with specific properties (such as health or strength) and methods (such as attack or defense). TypeScript enhances this model by enabling static typing, which allows us to define types for properties and method parameters.

class GameCharacter {
    // constructor method with type annotations
    constructor(public name: string, public health: number, public strength: number) {}

    attack(otherCharacter: GameCharacter): void {    // method with parameter type and return type annotations
        otherCharacter.health -= this.strength;
    }
}

TypeScript classes facilitate the grouping of associated code elements, simplifying their management. Now, to better understand how the above example works, let's go through it step-by-step.

Structure of a TypeScript Class

A TypeScript class serves as a blueprint consisting of properties and methods, with type annotations adding clarity and robustness. While properties represent data relevant to a class instance, methods are actions or functions that manipulate this data. Each class includes a constructor function, which is used to define class properties with type annotations. The constructor initializes the properties when an object is created, and the public keyword automatically creates and assigns the properties to the class instance, eliminating the need for separate declarations. Without public, we would have to declare and assign properties separately inside the constructor.

An essential keyword within these methods is this, which represents the class instance. In object-oriented programming, it's needed to access the class's properties and methods. When a new class instance is created, TypeScript automatically passes it to the this parameter to access individual instance properties and methods using the this keyword. This mechanism allows each object to keep track of its own state and behaviors.

class GameCharacter {
    // constructor: defines class properties with type annotations
    constructor(public name: string, public health: number, public strength: number) {}
}

const character = new GameCharacter("Hero", 100, 20);  // object or instance of the class
Class Properties

Properties in TypeScript classes hold data associated with each class instance. In our GameCharacter class, name, health, and strength are properties, each with a specified type. You can access a class property using the object of the class, followed by a dot (.), and then the property name.

class GameCharacter {
    // constructor: defines class properties with type annotations
    constructor(public name: string, public health: number, public strength: number) {}
}

const character = new GameCharacter("Hero", 100, 20);  // object or instance of the class
console.log(character.name);  // prints: Hero
console.log(character.health);  // prints: 100
console.log(character.strength);  // prints: 20

Using TypeScript's type system, properties ensure type safety by preventing unexpected types and bugs.

Class Methods

A class also contains methods — actions or functions that manipulate the data in the class, with parameters and return types explicitly defined. For example, the attack method in our GameCharacter class simulates an attack by one game character on another.

class GameCharacter {
    // constructor: defines class properties with type annotations
    constructor(public name: string, public health: number, public strength: number) {}

    attack(otherCharacter: GameCharacter): void {  // method with parameter type and return type annotations
        otherCharacter.health -= this.strength;  // modifies 'otherCharacter's health property
    }
}

const character1 = new GameCharacter("Hero", 100, 20);   // First instance of GameCharacter
const character2 = new GameCharacter("Villain", 80, 15);  // Second instance

console.log(character2.health);  // prints: 80
character1.attack(character2);  // character1 attacks character2
console.log(character2.health);  // prints: 60

Note: The keyword this refers to the instance of the class where the method is being invoked. For example, if character1.attack(character2) is called, this.strength refers to character1's strength property. This distinguishes properties of one instance from another, especially in interactions between objects like the attack method.

Examples of TypeScript Classes, Properties, and Methods
Lesson Summary and Practice

Great work exploring TypeScript classes, their properties, and methods. TypeScript classes help organize your code, improving its readability, manageability, and reliability with static typing. Now, test your understanding with exercise problems to solidify your newly gained knowledge. Happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal