Level Up Your TypeScript Classes with Getters and Setters

Getters and Setters in TypeScript make your class APIs clean, safe, and predictable. With them, you will have control over how values enter and leave your objects, reducing bugs and improving readability. In this guide, you will understand how to use them confidently with simple and real-life examples.

If you want to level up your TypeScript classes, accessors are a perfect place to start. You’ll see how small changes in your class design unlock validation, computed values, and better encapsulation.

When to choose getters and setters in TypeScript

The getters and setters in TypeScript can be used for similar cases as below:

  • You need validation or formatting on writes.
  • You want a computed value on reads.
  • You plan to change internals without breaking callers.
  • Enforce read-only or protected fields
  • Offer a clean, property-like developer experience

Example of getters and setters in TypeScript

Let’s take a simple example of getters and setters in TypeScipt. Below is a simple, real-life User example that validates email and computes a full name.

class User {
  private _firstName: string
  private _lastName: string
  private _email: string

  constructor(firstName: string, lastName: string, email: string) {
    this._firstName = firstName
    this._lastName = lastName
    this._email = email
  }

  get fullName(): string {
    return `${this._firstName} ${this._lastName}`
  }

  get email(): string {
    return this._email
  }

  set email(value: string) {
    if (!value.includes('@')) {
      throw new Error('Invalid email')
    }
    this._email = value.trim()
  }
}

const user = new User('Ava', 'Lee', 'ava@example.com')
user.email = 'ava.lee@company.com'
console.log(user.fullName)

In this example, the email getter method allows us to access the private email property of the user class without directly accessing it. Same goes for full name method return full name combining with first and last name. However the email setter method allow us to perform some additional operation before assigning value to the class like data validation and sanatization.

Let’s take another example to understand it better.

class Product {
  private _price: number

  constructor(price: number) {
    this._price = Math.max(0, Math.round(price * 100) / 100)
  }

  get price(): number {
    return this._price
  }

  set price(value: number) {
    if (value < 0) {
      throw new Error('Price cannot be negative')
    }
    this._price = Math.round(value * 100) / 100
  }
}

const book = new Product(9.995)
book.price = 12.5
console.log(book.price)

In this case, product class’s getter and setter methods ensure price is never negative and always stored with two decimals rather than given value.

Conclusion

Getters and Setters in TypeScript help you ship safer code without clutter. They add validation, protect data, and keep your API simple. Employ accessors to level up your TypeScript classes and make everyday scenarios like pricing, names, and units easy to maintain.