get Keyword in Typescript

Do you want to know about the get Keyword in Typescript? In this tutorial, I will explain how to use the get keyword in Typescript with examples and complete code.

The get keyword in TypeScript is used to define a getter method, allowing controlled access to a class’s properties. It’s useful for encapsulation, creating computed properties, and enhancing code readability. For example, in a Person class, a get fullName() method can return a concatenated full name from private _firstName and _lastName properties.

What is the get Keyword in TypeScript?

In TypeScript, the get keyword is used to define a getter method. A getter method is a special kind of method that is used to read the value of a property. Instead of accessing a property directly, you can use a getter method to provide controlled access to the value of a property.

Why use the get Keyword in Typescript

Here are the reasons we use the get keyword in Typescript.

  • Encapsulation: It helps in encapsulating the internal representation of a property.
  • Readability: Improves readability by accessing properties as if they are public, while actually implementing logic behind the scenes.
  • Control: Allows for additional logic to be executed when a property is accessed.

How to use the Typescript get keyword?

Now, let us see how to use the Typescript get keyword. Here is a complete example:

We have a Person class with private properties for the first and last names. We want to create a getter method to return the full name of the person without directly exposing the first and last name properties.

First, we define a class named Person with private properties _firstName and _lastName like the below code:

class Person {
    private _firstName: string;
    private _lastName: string;

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

    // Getter method to be added here
}

Now, let’s add a getter method named fullName using the get keyword.

class Person {
    // ... [previous code]

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

Finally, we create an instance of the Person class and use the fullName getter to access the person’s full name.

let person = new Person("John", "Doe");
console.log(person.fullName); // Output: John Doe

Here is the complete code:

class Person {
  private _firstName: string;
  private _lastName: string;

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

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

// Usage
const person = new Person("John", "Doe");
console.log(person.fullName);

In this example, fullName is a getter that concatenates the first and last names. Notice how we access it as a property (person.fullName) rather than a method (person.fullName()).

Once you run the code using VS code, you can see the screenshot below:

get Keyword in Typescript

Here are some best practices to use the get keyword in Typescript.

  • Naming Convention: The getter name should be meaningful and reflect the property it’s associated with.
  • Avoid Side Effects: Getters should not modify the state of the object. Their primary role is to return a value.
  • Type Annotations: Always use type annotations for clarity and type safety.

Conclusion

The get keyword in TypeScript is a powerful feature for creating getter methods in classes. It not only enhances the encapsulation of your classes but also makes your code more readable and maintainable. In this Typescript tutorial, I have explained how to use the get Keyword in Typescript.

You may also like:

>