100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
TypeScript

Getters and Setters in TypeScript

Use get and set accessors to expose class properties that run custom logic, such as validation, while still being accessed with plain property syntax.

Classes & OOPIntermediate7 min readJul 8, 2026
Analogies

1. Introduction

Getters and setters (accessors) let a class expose what looks like a normal property from the outside, while actually running a method behind the scenes. This makes it possible to compute derived values on read, or validate and transform values on write, without changing how callers interact with the property.

🏏

Cricket analogy: A Player class's strikeRate getter computes runs divided by balls faced on the fly whenever player.strikeRate is read, looking like a plain property to the caller even though a calculation runs behind the scenes.

2. Syntax

typescript
class ClassName {
  private _value: Type;

  get propertyName(): Type {
    return this._value;
  }

  set propertyName(newValue: Type) {
    this._value = newValue;
  }
}

const instance = new ClassName();
instance.propertyName;        // calls the getter
instance.propertyName = val;  // calls the setter

3. Explanation

A get accessor runs whenever the property is read, and a set accessor runs whenever the property is assigned a new value, but callers use ordinary property syntax (instance.propertyName), not method-call syntax. This is commonly paired with a private backing field (often prefixed with an underscore) that stores the real value, while the public getter/setter pair controls access to it.

🏏

Cricket analogy: Setting player.jerseyNumber = 18 runs a set jerseyNumber(value) accessor behind the scenes, storing it in a private _jerseyNumber field, while the caller writes ordinary property syntax rather than calling a method like setJerseyNumber(18).

Setters are a natural place to add validation logic — for example, rejecting an invalid value by throwing an error before it's ever stored. A class can also define a getter with no matching setter, in which case the property becomes effectively read-only from the outside, even though it isn't declared with the readonly keyword.

🏏

Cricket analogy: A set battingAverage(value) accessor can throw an error before storing a negative average, and a Player class that only defines get careerRuns() with no matching setter makes that stat effectively read-only, even without the readonly keyword.

If a class defines only a get accessor for a property with no matching set accessor, that property becomes effectively readonly from outside the class — any attempt to assign to it (e.g. instance.fahrenheit = 100) is a compile-time error, even though the readonly keyword was never used.

4. Example

typescript
class Temperature {
  private _celsius: number;

  constructor(celsius: number) {
    this._celsius = celsius;
  }

  get celsius(): number {
    return this._celsius;
  }

  set celsius(value: number) {
    if (value < -273.15) {
      throw new Error("Temperature below absolute zero");
    }
    this._celsius = value;
  }

  get fahrenheit(): number {
    return (this._celsius * 9) / 5 + 32;
  }
}

const t = new Temperature(25);
console.log(t.celsius);
console.log(t.fahrenheit);

t.celsius = 30;
console.log(t.celsius);
console.log(t.fahrenheit);

5. Output

text
25
77
30
86

6. Key Takeaways

  • get and set accessors let a property run custom logic while still being accessed with plain property syntax.
  • Setters are a natural place for validation before storing a value in a private backing field.
  • A getter with no matching setter makes a property effectively readonly from outside the class.
  • Getters can compute derived values on the fly, like fahrenheit derived from _celsius.
  • Backing fields (e.g. _celsius) are conventionally private and prefixed with an underscore.

Practice what you learned

Was this page helpful?

Topics covered

#TypeScript#TypeScriptProgrammingStudyNotes#Programming#GettersAndSettersInTypeScript#Getters#Setters#Syntax#Explanation#StudyNotes#SkillVeris