What is the accessor keyword in TypeScript 4.9?

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.