Getters and Setters in TypeScript: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Get and Set in TypeScript: The Magic of Accessors Without the Pain

\n\n

Have you ever written a class in TypeScript and thought: "I wish I could make a property smart, so that when reading it calculates something, and when writing it validates the data"? Meet getters (get) and setters (set). They are not just syntactic sugar, but a powerful tool for controlling data access.

\n\n

If you've already encountered this topic on Stack Overflow, you know: the question "get and set in TypeScript" gathers thousands of views. People get confused about when to use accessors and when to use regular methods. In this article, we'll break everything down: from the basics to tricky nuances.

\n\n

Ready? Let's go!

\n\n

What are get and set in TypeScript?

\n\n

A getter is a method that is called when reading a property. A setter is a method called when assigning a value. Together they form accessors. It looks like you are working with a regular field, but under the hood there is logic.

\n\n

Here is a minimal example:

\n\n
class User {\n  private _name: string = '';\n\n  get name(): string {\n    return this._name;\n  }\n\n  set name(value: string) {\n    if (value.length > 0) {\n      this._name = value;\n    } else {\n      console.log('Name cannot be empty');\n    }\n  }\n}\n\nconst user = new User();\nuser.name = 'Anna'; // calls set\nconsole.log(user.name); // calls get → 'Anna'\nuser.name = ''; // attempt to set an empty name → console message
\n\n

Note: the _name property with the _ prefix is a private field. Direct access from outside is impossible. Instead, we use the public accessor name.

\n\n

Why do you need getters and setters? 3 main reasons

\n\n

1. Data Validation

\n

A setter is the perfect place to check input values. You can reject invalid data before it enters the object.

\n\n
class Temperature {\n  private _celsius: number = 0;\n\n  get celsius(): number {\n    return this._celsius;\n  }\n\n  set celsius(value: number) {\n    if (value < -273.15) {\n      throw new Error('Temperature below absolute zero is impossible');\n    }\n    this._celsius = value;\n  }\n}
\n\n

2. Computed Properties

\n

A getter can return not a stored value, but the result of calculations based on other data.

\n\n
class Rectangle {\n  constructor(public width: number, public height: number) {}\n\n  get area(): number {\n    return this.width * this.height;\n  }\n\n  get perimeter(): number {\n    return 2 * (this.width + this.height);\n  }\n}\n\nconst rect = new Rectangle(10, 5);\nconsole.log(rect.area); // 50 — computed on the fly
\n\n

3. Encapsulation and Access Control

\n

You can make a property read-only (getter only) or write-only (setter only). This helps adhere to OOP principles.

\n\n
class Config {\n  private _apiKey: string = 'secret123';\n\n  get apiKey(): string {\n    // Can add logging or permission checks\n    return this._apiKey;\n  }\n  // Setter not defined — property is read-only\n}\n\nconst config = new Config();\nconsole.log(config.apiKey); // 'secret123'\n// config.apiKey = 'newKey'; // Compilation error!
\n\n

Frequently Asked Questions about get and set in TypeScript

\n\n

Let's go over what beginners (and not only) most often ask on Stack Overflow and forums.

\n\n

Question 1: How are get/set different from regular methods?

\n

The main difference is call syntax. Accessors look like properties: without parentheses. This makes the code cleaner and more intuitive. Compare:

\n\n
// With methods\nuser.getName();\nuser.setName('Peter');\n\n// With accessors\nuser.name;\nuser.name = 'Peter';
\n\n

The second difference is that accessors cannot be overloaded. Each accessor has one signa

Blogs

Book Recommendations