VOOZH about

URL: https://www.geeksforgeeks.org/typescript/how-to-use-getters-setters-in-typescript/

⇱ How to use getters/setters in TypeScript ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to use getters/setters in TypeScript ?

Last Updated : 23 Jan, 2025

In TypeScript, getters and setters provide controlled access to class properties, enhancing encapsulation and flexibility.

  • Getters allow you to retrieve the value of a property with controlled logic.
  • Setters enable controlled assignment to properties, often including validation or transformations.
  • The get method allows controlled access to the _name property by returning its value.
  • The set method enables controlled modification of the _name property, applying logic to ensure valid input.

Output:

Alice
Bob

1. Getter Method in TypeScript

A getter method in TypeScript is a special method that allows you to retrieve the value of an object's property in a controlled way. It is used to define how a property value is accessed.

  • Getters provide a way to access private or protected class members without directly exposing them.
  • The getter method can include logic to manipulate or validate the value before returning it.

Syntax:

get propertyName(): returnType {
// logic to return a value
}
  • propertyName: The name of the property you want to access.
  • returnType: The type of the value returned by the getter.
  • The area getter method calculates the area of the rectangle and returns it whenever accessed.
  • The area property is read-only because there is no setter method defined, providing a safe way to retrieve computed values without modification.

Output:

50

2. Setter Method in TypeScript

A setter method in TypeScript allows controlled modification of a class's property. It enables encapsulation by providing a mechanism to validate or manipulate data before assigning it to a private field.

  • Setters are defined using the set keyword followed by the property name and a parameter representing the new value.
  • They are useful for enforcing constraints or triggering side effects when a property value changes.

Syntax:

set propertyName(value: type) {
// logic to set the value
}
  • The Employee class has a private property _fullName to store the employee's name.
  • The fullName getter retrieves the current value of _fullName.
  • The fullName setter validates the newName to ensure it's a non-empty string before assigning it to _fullName. If the validation fails, it logs an error message.

Output:

John Doe
Invalid name.

More Example of Using Getter and Setter

Validating Age Property

  • The User class has a private _age property to store the user's age.
  • The age getter returns the current value of _age.
  • The age setter validates the input to ensure it's within a realistic range (1 to 149) before assigning it to _age. If the input is invalid, an error message is logged.

Output:

25
Invalid age value

Full Name Property with Getter and Setter

  • The Person class has private _firstName and _lastName properties.
  • The fullName getter concatenates _firstName and _lastName to return the full name.
  • The fullName setter splits the input string and assigns the parts to _firstName and _lastName. It expects exactly two parts; otherwise, it logs an error.

Output:

John Doe
Invalid full name format
Comment
Article Tags:

Explore