Getters and setters
get syntax
A getter looks like a field to callers but runs a method behind the scenes. Use get to compute a value from current state on every read.
class Temp { celsius: number = 0; get fahrenheit(): number { return this.celsius * 1.8 + 32; } } const temp = new Temp(); console.log(temp.fahrenheit);
32
set with validation
A setter runs on every write, so it is the right place to check values. Reject bad input before it reaches the stored state.
class User { private ageValue: number = 0; get age(): number { return this.ageValue; } set age(value: number) { if (value >= 0) { this.ageValue = value; } } } const user = new User(); user.age = 30; user.age = -5; console.log(user.age);
30
Backing fields
A backing field holds the real data while the accessor controls the door. The accessor must read and write the backing field, never itself, or the call will loop forever.
class User { private nameValue: string = "Guest"; get name(): string { return this.nameValue; } set name(value: string) { this.nameValue = value.trim(); } } const user = new User(); user.name = " Ada "; console.log(user.name);
Ada
When accessors win
Reach for accessors when reads need computing or writes need guarding. Plain fields stay better for simple storage with no rules attached.
Start with a plain field and upgrade to a getter and setter only when real logic appears.
class Order { count: number = 1; price: number = 10; get total(): number { return this.count * this.price; } } const order = new Order(); order.count = 3; console.log(order.total);
30
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How does a caller read a value behind a getter?
Answer
As a field, as in
temp.fahrenheitA getter keeps the field syntax for the caller, while the class runs a method on every read.
-
Where does a validation rule for a written value belong?
Answer
In the setter, so every write passes through it
The setter runs on every assignment, so bad input is rejected before it reaches the stored state.
-
What is a backing field?
Answer
A private field that holds the data an accessor guards
The accessor reads and writes the private backing field, and the field name stays hidden from callers.
-
What happens when a setter assigns to its own name, as
set age(value) { this.age = value; }?Answer
The call loops until the stack overflows
Each assignment calls the setter again, so the class needs a separate backing field.
-
What does this program print?
class Temp { celsius: number = 100; get fahrenheit(): number { return this.celsius * 1.8 + 32; } } const temp = new Temp(); console.log(temp.fahrenheit);Answer
212
100 times 1.8 is 180, and 32 more makes 212.
-
What does this program print?
class User { private ageValue: number = 0; get age(): number { return this.ageValue; } set age(value: number) { if (value >= 0) { this.ageValue = value; } } } const user = new User(); user.age = 30; user.age = -5; console.log(user.age);Answer
30
The setter keeps the first value and ignores the negative one, so the getter still returns 30.
-
What does this program print?
class User { private nameValue: string = "Guest"; get name(): string { return this.nameValue; } set name(value: string) { this.nameValue = value.trim(); } } const user = new User(); user.name = " Ada "; console.log(user.name);Answer
Ada
The setter trims the spaces before storing, so the getter returns the trimmed name.
-
What does this program print?
class Order { count: number = 1; price: number = 10; get total(): number { return this.count * this.price; } } const order = new Order(); order.count = 3; console.log(order.total);Answer
30
The getter multiplies the current count by the price on every read, so 3 times 10 is 30.
-
What does the compiler say about this program?
class Temp { get celsius(): number { return 0; } } const t = new Temp(); t.celsius = 5; console.log(t.celsius);Answer
error TS2540: Cannot assign to 'celsius' because it is a read-only property.
A getter with no setter is read-only, so the compiler refuses the assignment.
-
What does the compiler say about this program?
class User { private ageValue = 0; set age(a: number, b: number) { this.ageValue = a + b; } } const u = new User(); u.age = 5; console.log("done");Answer
error TS1049: A 'set' accessor must have exactly one parameter.
A setter receives exactly one value, and the compiler rejects the second parameter.
Article author: Arthur Isaev