0
0
Typescriptprogramming~10 mins

Getter and setter with types in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Getter and setter with types
Create class with private property
Define getter method
Define setter method with type check
Create instance
Use getter to read value
Use setter to update value
Value updated with type safety
The flow shows creating a class with a private property, then defining typed getter and setter methods to safely read and update the property.
Execution Sample
Typescript
class Person {
  private _age: number;

  constructor(age: number) {
    this._age = age;
  }

  get age(): number {
    return this._age;
  }

  set age(value: number) {
    if (value >= 0) {
      this._age = value;
    }
  }
}

const p = new Person(30);
console.log(p.age); // 30
p.age = 35;
console.log(p.age); // 35
p.age = -5; // ignored
console.log(p.age); // 35
This code defines a Person class with a private age property and typed getter/setter to control access and enforce non-negative age.
Execution Table
StepActionValue of _ageGetter OutputSetter InputSetter Effect
1Create Person instance with age=3030
2Call getter p.age3030
3Call setter p.age = 353035Updated _age to 35
4Call getter p.age3535
5Call setter p.age = -535-5Ignored (negative value)
6Call getter p.age3535
💡 Setter ignores negative values; _age remains 35 after invalid set.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
_ageundefined30353535
Key Moments - 2 Insights
Why does the age not change when setting it to -5?
Because the setter checks if the value is negative and ignores it if so, as shown in execution_table step 5.
Why do we use a private property _age instead of a public one?
To control access and enforce rules via getter and setter, preventing invalid values from being set, as seen in the setter logic.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what happens to _age?
AIt changes to -5
BIt changes from 30 to 35
CIt stays 30
DIt becomes undefined
💡 Hint
Check the 'Value of _age' and 'Setter Effect' columns at step 3 in execution_table.
At which step does the setter ignore the input value?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for 'Ignored (negative value)' in the 'Setter Effect' column in execution_table.
If we remove the type check in the setter, what could happen?
ANegative ages could be set
BGetter would stop working
CPrivate property becomes public
DConstructor would fail
💡 Hint
Consider the setter logic and what happens when invalid values are not checked.
Concept Snapshot
class MyClass {
  private _prop: Type;
  get prop(): Type { return this._prop; }
  set prop(value: Type) {
    // optional validation
    this._prop = value;
  }
}
Use getters/setters to control access with type safety and validation.
Full Transcript
This example shows how to create a class with a private property and use getter and setter methods with types in TypeScript. The getter returns the private value, and the setter updates it only if the new value passes a check (non-negative in this case). The execution table traces creating an instance, reading the value, updating it with valid and invalid inputs, and shows how the setter enforces rules. The variable tracker shows how the private property changes over time. Key moments clarify why invalid values are ignored and why private properties are used. The quiz tests understanding of these steps and effects.