Using Getters and Setters in TypeScript

Tutorial 5 of 5

Tutorial: Using Getters and Setters in TypeScript

1. Introduction

1.1 Goal of the Tutorial

In this tutorial, we will learn how to use getters and setters in TypeScript. Getters and setters are special methods that provide read and write access to an object's properties respectively.

1.2 Learning Outcomes

By the end of this tutorial, you will be able to:
- Understand the concept of getters and setters in TypeScript.
- Use getters and setters to control access to class properties.
- Apply best practices when using getters and setters.

1.3 Prerequisites

To get the most out of this tutorial, you should have a basic understanding of TypeScript and classes in TypeScript.

2. Step-by-Step Guide

2.1 Getters in TypeScript

In TypeScript, a getter is a method that is used to retrieve the value of an object's property. It provides a way to access the private properties of a class.

Here's how to declare a getter in TypeScript:

class MyClass {
    private _myProperty: string;

    get myProperty(): string {
        return this._myProperty;
    }
}

In the above code, myProperty is a getter that returns the value of _myProperty.

2.2 Setters in TypeScript

A setter, on the other hand, is a method that is used to set the value of an object's property. It provides a way to modify the private properties of a class.

Here's how to declare a setter in TypeScript:

class MyClass {
    private _myProperty: string;

    set myProperty(value: string) {
        this._myProperty = value;
    }
}

In the above code, myProperty is a setter that sets the value of _myProperty.

2.3 Best Practices

  • Use getters and setters to encapsulate and protect your data. A getter and setter provide a controlled way of accessing and modifying data.
  • Prefix private variables with an underscore (_). This is a common convention to indicate that a variable is intended to be private.

3. Code Examples

3.1 Example 1: Using a Getter

class Person {
    private _name: string;

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

    // Getter for name
    get name(): string {
        return this._name;
    }
}

let person1 = new Person('John');
console.log(person1.name); // Output: John

3.2 Example 2: Using a Setter

class Person {
    private _name: string;

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

    // Setter for name
    set name(value: string) {
        this._name = value;
    }
}

let person1 = new Person('John');
person1.name = 'Jane';
console.log(person1.name); // Output: Jane

4. Summary

In this tutorial, we have learned about getters and setters in TypeScript. We saw how they are used to protect and encapsulate class properties. We also went through some best practices to follow when using getters and setters.

5. Practice Exercises

5.1 Exercise 1:

Create a class Student with a private property _grade. Implement a getter and a setter for grade that ensures the grade is between 0 and 100.

5.2 Exercise 2:

Create a class Rectangle with private properties _length and _width. Implement getters and setters for length and width. Also, implement a getter for area which calculates the area of the rectangle.

5.3 Exercise 3:

Create a class BankAccount with a private property _balance. Implement a getter for balance. Implement a deposit(amount) method and a withdraw(amount) method, ensuring that the balance never goes negative.

Please note that these exercises are meant to test your understanding of getters and setters in TypeScript. I encourage you to try them out on your own before looking up solutions. Happy coding!