An auto-accessor field is a class property declaration prefixed with theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
accessor keyword that automatically generates a private backing field along with a corresponding getter and setter pair on the class prototype. Introduced to align TypeScript with the ECMAScript Class Accessors proposal, it provides a standardized mechanism for property interception, specifically designed to be targeted by class decorators.
Syntax
Theaccessor modifier is placed before the property name, optionally accompanied by a type annotation and an initial value.
Underlying Mechanics
When the TypeScript compiler processes an auto-accessor, it desugars the single declaration into three distinct components:- A private, uniquely named backing field on the class instance.
- A
getmethod on the class prototype. - A
setmethod on the class prototype.
Technical Characteristics
- Memory Allocation: Unlike standard class fields which are defined directly on the instance (
this.propertyName), the auto-accessor defines its getter and setter on the prototype chain. Only the hidden backing field consumes per-instance memory. - Initialization Phase: The default value assigned to an auto-accessor is evaluated and bound to the private backing field during the instance initialization phase, immediately prior to the execution of the constructor body.
- Decorator Metadata: When a decorator is applied to an
accessorfield, the decorator’s context receives aClassAccessorDecoratorContext. Instead of receiving a standard property descriptor, the decorator receives an object containinggetandsetfunctions. This allows the decorator to wrap or mutate the accessor logic directly without needing to manually redefine the property descriptor usingObject.defineProperty.
Interface Implementation
Auto-accessors satisfy interface requirements identically to standard properties or explicit getter/setter pairs. The interface only dictates the public contract, remaining agnostic to the underlyingaccessor implementation.
Master TypeScript with Deep Grasping Methodology!Learn More





