What is the accessor keyword in TypeScript 4.9?

Why Interviewers Ask This

Interviewers ask this to evaluate whether you have the depth of knowledge needed to mentor others and lead technical decisions. The expected answer goes beyond definitions into practical implications and real-world consequences.

Answer

TypeScript 4.9 introduced the accessor keyword for class fields, which automatically generates getter and setter methods using the ECMAScript accessor property proposal. Syntax: class Person { accessor name: string = ""; }. This is shorthand for: class Person { private _name: string = ""; get name() { return this._name; } set name(value: string) { this._name = value; } }. The generated getter and setter use a private backing field (implementation details vary). Benefits: cleaner syntax for reactive properties, better integration with decorators (the @accessor decorator can intercept get/set operations), and consistent behavior across class hierarchies. The accessor keyword is particularly useful with the new decorator standard (stage 3) where @accessor decorators can add validation, change notification, or transformation logic to individual properties. It enables patterns like observable properties in UI frameworks without verbose boilerplate.

Pro Tip

Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex TypeScript answers easy to follow.