Wrap computed or validated access around fields.
Section: Classes
Use getters and setters
typescript
typescript
class Temperature {
private celsius = 0;
get fahrenheit() {
return this.celsius * 1.8 + 32;
}
set fahrenheit(value: number) {
this.celsius = (value - 32) / 1.8;
}
}Explanation
Getters and setters let APIs expose properties while keeping internal conversions hidden.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Classes
Use access modifiers
Control visibility with `public`, `private`, and `protected`.
Use an abstract class
Require derived classes to implement specific methods.