How to make a getter method in a class in TypeScript?
Tags: typescript
Published: 2022-04-22 00:00:00

---
  title: How to make a getter method in a class in TypeScript?
  published: true
  tags: TypeScript
  date: Fri Apr 22 2022 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/make-getter-method-class-typescript
  cover_image: https://melvingeorge.me/_next/static/images/main-0eb9a8d3b1a5b615823acf7b657bd9de.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/make-getter-method-class-typescript)
  
To make a `getter` method in a [class](https://melvingeorge.me/blog/create-class-typescript) in TypeScript, you can use the `get` keyword followed by the [method definition](https://melvingeorge.me/blog/create-methods-class-typescript) in the class.

**A `getter` method is used to access the value of a particular class field. Mostly if you have some processing to do before returning the value.**

### TL;DR

```ts
// a simple class
class Car {
  _name: string;
  yearMade: number;

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

  // getter method for the `_name` class field
  get name() {
    return this._name;
  }
}

// create an instance of the `Car` class
const lexus = new Car("Lexus", 2022);

// cool part is that we can use
// `name` like how we access a property
//  note that we are not invoking the
// `name` even thought it's a "method" 👇🏽
console.log(lexus.name); // Lexus ✅
```

For example, let's say we have a class called `Car` with 2 [fields](https://melvingeorge.me/blog/create-field-class-typescript) called `name` having the type of string and `yearMade` having the type of `number` and a [constructor](https://melvingeorge.me/blog/create-class-constructor-typescript) to initialize the values of the fields in the class like this,

```ts
// a simple class
class Car {
  name: string;
  yearMade: number;

  constructor(name: string, yearMade: number) {
    this.name = name;
    this.yearMade = yearMade;
  }
}
```

Now let's make a getter method for the `name` class field. To do that we can start by first creating a method that returns the `name` field value using the `this` operator and also prefixing the method with the keyword `get`. Yes, that's all there is to do to make a `getter` method.

It can be done like this,

```ts
// a simple class
class Car {
  name: string;
  yearMade: number;

  constructor(name: string, yearMade: number) {
    this.name = name;
    this.yearMade = yearMade;
  }

  // getter method for the `name` class field
  get getName() {
    return this.name;
  }
}
```

Now let's create an instance of the `Car` class and see how it works (hold on. there is a surprise 🙈),

```ts
// a simple class
class Car {
  name: string;
  yearMade: number;

  constructor(name: string, yearMade: number) {
    this.name = name;
    this.yearMade = yearMade;
  }

  // getter method for the `name` class field
  get getName() {
    return this.name;
  }
}

// create an instance of the `Car` class
const lexus = new Car("Lexus", 2022);

// cool part is that we can use
// `getName` like how we access a property
//  note that we are not invoking the
// `getName` even thought it's a "method" 👇🏽
console.log(lexus.getName); // Lexus ✅
```

If you look closely at the above code you can see that we are using the `getName` like how we access just a normal property in an object and not invoking it even though it's a method. Every getter method can be accessed like a property.

Yeah, this is the surprise I was talking about. Cool Right 🤩!

Now you may think that why do we even go for the hassle in making the `getter` method for the `name` field? We can simply access it from the instance of the class.

But we cannot do that if it is a `private` field. We will be discussing more on `private` class fields in the upcoming blogs.

**The main big reason is to have some logic run or some processing done before accessing the field itself.**

Now for naming purposes let's rename the `name` field to `_name` and the `getName` getter method to just the `name` since we are accessing it as a property on the object it's best to follow these conventions.

it can be done like this,

```ts
// a simple class
class Car {
  _name: string;
  yearMade: number;

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

  // getter method for the `_name` class field
  get name() {
    return this._name;
  }
}

// create an instance of the `Car` class
const lexus = new Car("Lexus", 2022);

// cool part is that we can use
// `name` like how we access a property
//  note that we are not invoking the
// `name` even thought it's a "method" 👇🏽
console.log(lexus.name); // Lexus ✅
```

We have successfully created a `getter` method for a class field in TypeScript. Yay 🥳!

See the above code live in [codesandbox](https://codesandbox.io/s/make-getter-method-class-typescript-k4nh6z?file=/src/index.ts).

That's all 😃!

### Feel free to share if you found this useful 😃.

---