Getters and Setters with Types
What you'll learn: How TypeScript infers types from getters and setters, and how to explicitly type accessor methods in classes.
What Are Typed Getters and Setters?
In TypeScript classes, you can define getters (methods that retrieve values) and setters (methods that assign values) just like in JavaScript. What makes them special in TypeScript is that they participate fully in the type system: TypeScript will infer the property type from your getter's return type, and you can add explicit annotations to control exactly what types go in and out.
Think of getters and setters as doorways with guards: the getter controls what comes out (the return type), and the setter controls what goes in (the parameter type).
Basic Getter Type Inference
When you create a getter, TypeScript infers the property's type from what the getter returns:
class Temperature {
private _celsius: number = 0;
get fahrenheit() {
return this._celsius * 9/5 + 32;
}
}
const temp = new Temperature();
const f = temp.fahrenheit; // Type inferred as number
TypeScript sees the getter returns a number, so accessing .fahrenheit gives you a number type.
Basic Setter Type Inference
Setters determine what type can be assigned to a property:
class Temperature {
private _celsius: number = 0;
set celsius(value: number) {
this._celsius = value;
}
}
const temp = new Temperature();
temp.celsius = 25; // ✓ OK
temp.celsius = "25"; // ✗ Error: Type 'string' is not assignable to type 'number'
Getter and Setter Together
When you pair a getter and setter, TypeScript ensures type consistency between them:
class User {
private _name: string = "";
get name(): string {
return this._name;
}
set name(value: string) {
this._name = value.trim();
}
}
The property type becomes string because both the getter returns string and the setter accepts string.
Asymmetric Getter/Setter Types
You can have different types for getters and setters, though this is less common:
class StringBuffer {
private data: string[] = [];
get content(): string {
return this.data.join("");
}
set content(value: string | string[]) {
this.data = typeof value === "string" ? [value] : value;
}
}
const buffer = new StringBuffer();
buffer.content = "hello"; // ✓ accepts string
buffer.content = ["hi", "bye"]; // ✓ accepts string[]
const text: string = buffer.content; // ✓ returns string
Readonly Through Getter-Only
A getter without a matching setter creates a readonly property:
class Circle {
constructor(private radius: number) {}
get area(): number {
return Math.PI * this.radius ** 2;
}
}
const c = new Circle(5);
const a = c.area; // ✓ OK
c.area = 100; // ✗ Error: Cannot assign to 'area' because it is a read-only property
When Type Inference Works
TypeScript is quite smart about inferring accessor types. You typically don't need explicit annotations unless:
- You want to document the intended type clearly
- You want to catch errors in the getter/setter implementation
- The inferred type is too narrow or too wide
class Config {
private _timeout = 5000;
// Explicit annotation ensures we always return number
get timeout(): number {
return this._timeout;
}
set timeout(ms: number) {
if (ms < 0) throw new Error("Invalid timeout");
this._timeout = ms;
}
}
Key Takeaway: TypeScript automatically infers property types from getter return types and setter parameter types, making accessors fully type-safe while allowing you to add validation logic and computed properties to your classes.