Get and Set in TypeScript: The Magic of Accessors Without the Pain
\n\nHave 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\nIf 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\nReady? Let's go!
\n\nWhat are get and set in TypeScript?
\n\nA 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\nHere is a minimal example:
\n\nclass 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\nNote: the _name property with the _ prefix is a private field. Direct access from outside is impossible. Instead, we use the public accessor name.
Why do you need getters and setters? 3 main reasons
\n\n1. Data Validation
\nA setter is the perfect place to check input values. You can reject invalid data before it enters the object.
\n\nclass 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\n2. Computed Properties
\nA getter can return not a stored value, but the result of calculations based on other data.
\n\nclass 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\n3. Encapsulation and Access Control
\nYou can make a property read-only (getter only) or write-only (setter only). This helps adhere to OOP principles.
\n\nclass 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\nFrequently Asked Questions about get and set in TypeScript
\n\nLet's go over what beginners (and not only) most often ask on Stack Overflow and forums.
\n\nQuestion 1: How are get/set different from regular methods?
\nThe 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\nThe second difference is that accessors cannot be overloaded. Each accessor has one signa