TypeScript Reference ยท Keywords and Declarations
The accessor keyword
Reference for the TypeScript accessor keyword: declare auto-accessor class fields that create a private backing slot with a getter and setter.
The accessor keyword in front of a class field declares an auto-accessor. The compiler (or the JavaScript engine, once the feature ships natively) turns the field into a private storage slot plus a public getter and setter pair that read and write that slot. From the outside it looks and behaves like a normal property; on the prototype it is a real accessor property.
It was introduced for decorators: a decorator applied to an auto-accessor can wrap the getter and setter without you having to write them by hand. TypeScript supports the syntax since version 4.9, and it needs no decorators at all to be useful.
class Counter { accessor count = 0; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
class C { [modifiers] accessor name [?|!] [: Type] [= initializer]; static accessor name = value; }
| Part | Description |
|---|---|
accessor |
Marks the field as an auto-accessor. It goes after modifiers such as public, protected, private, static and override, and directly before the name. |
| Name | Any property name, including computed names and #private names. |
: Type and initializer |
Same rules as an ordinary field. The type is inferred from the initializer when omitted. |
Edge cases and errors:
- An
accessordeclaration is only valid as a class member; it is not allowed in interfaces or object literals. - The compiler lowers it to a private slot plus getter and setter, so the emitted JavaScript depends on
target. Do not rely on the shape of the generated code. - The hidden slot is not reachable from outside the class, not even with a string key.
Usage notes
- A normal field is an own property of each instance. An auto-accessor puts the getter and setter on the prototype and keeps the value in hidden per-instance storage, so
Object.keys(instance)does not list it. - Because the accessor lives on the prototype, subclasses can override it with their own getter and setter, which is not possible with plain fields in the same way.
- The main use case is decorators. A class auto-accessor decorator receives
{ get, set }and may return replacements, which is how reactive or validated properties are built. - Without decorators,
accessoradds little over a normal field. If you need validation or side effects today, write explicitgetandsetmembers, which you control completely. - Do not confuse
accessorwith thegetandsetkeywords. Those define the logic yourself;accessorgenerates it. - The initializer runs during construction, like a field initializer: in a base class it runs before the constructor body.