Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Decorator

AccessorDecorator

An accessor decorator runs on a getter or setter’s property descriptor, letting code observe, modify, or replace the accessor definition. It receives the owning constructor or prototype, the member name, and the descriptor.

How it works

An accessor decorator is placed immediately before a getter or setter. At runtime, TypeScript calls it with the class constructor or prototype, the member name, and the accessor’s property descriptor, allowing the descriptor to be inspected, changed, or replaced.

Syntax

function decorator(target: Function | object, name: string | symbol, descriptor: PropertyDescriptor): void | PropertyDescriptor

Parameters

NameTypeDescription
targetFunction | objectThe class constructor for a static accessor, or the class prototype for an instance accessor.
namestring | symbolThe accessor’s property name.
descriptorPropertyDescriptorThe property descriptor shared by the accessor member.

Returns

The decorator may return a property descriptor to replace the accessor definition; it may also return nothing.

Key points

  • Place the decorator directly before the getter or setter declaration.
  • The decorator operates on the accessor’s property descriptor.
  • The target is the constructor for static accessors and the prototype for instance accessors.
  • The second runtime argument identifies the decorated member.
  • The third runtime argument contains the member’s descriptor.

Common mistakes

  • Decorate only one accessor when a member has both a getter and setter.
  • Accessor decorators are unavailable in declaration files and ambient declarations.

Example: Changing an accessor descriptor

function configurable(value: boolean) {
  return function (target: object, name: string | symbol, descriptor: PropertyDescriptor) {
    descriptor.configurable = value;
  };
}

class Service {
  private _enabled = true;

  @configurable(false)
  get enabled() {
    return this._enabled;
  }
}

console.log(new Service().enabled);

The decorator receives the instance prototype, accessor name, and shared property descriptor, then changes the descriptor’s configurability.

When to use it

Adjust descriptor flags

Change whether an accessor can be reconfigured after class definition.

Customize accessor implementation

Wrap or replace getter/setter behavior through the descriptor.

Registration and instrumentation

Inspect the owning class and member name while processing class definitions.

Interview questions

QWhat does an accessor decorator do?

An accessor decorator is declared immediately before an accessor and is applied to its property descriptor. It can observe, modify, or replace the accessor definition.

Follow-up: Why is the descriptor shared by a getter and setter? Because the descriptor represents the combined property rather than either accessor declaration independently.

QWhat arguments does an accessor decorator receive?

For an instance accessor, the first argument is the class prototype; for a static accessor, it is the constructor. The second argument is the member name, and the third is the property descriptor.

Follow-up: What must a decorator change to affect configurability? It should update the relevant descriptor property before the decorated member is installed.

QWhy can’t both accessors of one property be decorated?

A member with both get and set accessors must have its decorators placed on only the first accessor in document order. Decorating both is rejected because both declarations use one property descriptor.

Follow-up: How should a pair of accessors be decorated? Put the decorator on the first accessor and treat it as applying to the whole property.

  • Decorator
  • Property Descriptor
  • get accessor
  • set accessor

Source: Documentation - Decorators - TypeScript. Checked 2026-10-15.