Decorator helpers provide comprehensive support for JavaScript decorators across multiple proposal stages, from legacy decorators to the current TC39 specification, including class and method decoration patterns.
Applies decorators using the 2023-11 specification (current stage 3 proposal).
/**
* Applies decorators (2023-11 spec - current)
* @param targetClass - Class being decorated
* @param memberDecs - Member decorator arrays
* @param classDecs - Class decorator array
* @param classDecsHaveThis - Whether class decorators need this binding
* @param instanceBrand - Brand for private member access
* @returns [enhanced class, instance extra initializers, static extra initializers]
*/
function applyDecs2311(
targetClass: Function,
memberDecs: any[][],
classDecs: any[],
classDecsHaveThis: boolean,
instanceBrand: Function
): [Function, Function[], Function[]];Usage Example:
import _applyDecs2311 from '@babel/runtime/helpers/esm/applyDecs2311';
// Used by Babel for current decorator syntax:
// @logged class MyClass { @validate method() {} }
const [DecoratedClass, instanceInitializers, staticInitializers] = _applyDecs2311(
MyClass,
[[validateDecorator], []], // member decorators
[loggedDecorator], // class decorators
false, // class decorators don't need this
Symbol('MyClass') // instance brand
);Applies decorators using the 2023-05 specification.
/**
* Applies decorators (2023-05 spec)
* @param targetClass - Class being decorated
* @param memberDecs - Member decorator configuration
* @param classDecs - Class decorator array
* @param classDecsHaveThis - Whether class decorators need this
* @param instanceBrand - Brand for private members
* @returns [enhanced class, instance initializers, static initializers]
*/
function applyDecs2305(
targetClass: Function,
memberDecs: any[],
classDecs: any[],
classDecsHaveThis: boolean,
instanceBrand: Function
): [Function, Function[], Function[]];Applies decorators using the 2023-01 specification.
/**
* Applies decorators (2023-01 spec)
* @param targetClass - Class being decorated
* @param memberDecs - Member decorator configuration
* @param classDecs - Class decorator array
* @param instanceBrand - Brand for private members
* @returns [enhanced class, instance initializers, static initializers]
*/
function applyDecs2301(
targetClass: Function,
memberDecs: any[],
classDecs: any[],
instanceBrand: Function
): [Function, Function[], Function[]];Applies decorators using the 2022-03 revised specification.
/**
* Applies decorators (2022-03 revised spec)
* @param targetClass - Class being decorated
* @param memberDecs - Member decorator configuration
* @param classDecs - Class decorator array
* @returns [enhanced class, instance initializers, static initializers]
*/
function applyDecs2203R(
targetClass: Function,
memberDecs: any[],
classDecs: any[]
): [Function, Function[], Function[]];Applies decorators using the 2022-03 specification.
/**
* Applies decorators (2022-03 spec)
* @param Class - Class being decorated
* @param decorators - All decorators to apply
* @returns Enhanced class with applied decorators
*/
function applyDecs2203(Class: Function, decorators: any[]): Function;Applies decorators using the stage 1 proposal (legacy).
/**
* Applies decorators (Stage 1 - legacy)
* @param targetClass - Class being decorated
* @param props - Property decorators
* @param decorators - Class decorators
* @returns Enhanced class with decorators applied
*/
function applyDecs(targetClass: Function, props: any[], decorators: any[]): Function;Legacy decorator implementation for older decorator syntax.
/**
* Legacy decorator implementation
* @param decorators - Array of decorator functions
* @param factory - Factory function for creating decorated element
* @param superClass - Super class for inheritance
* @param mixins - Mixin objects to apply
* @returns Decorated class or element
*/
function decorate(decorators: Function[], factory: Function, superClass?: Function, mixins?: any[]): any;Applies decorators to property descriptors.
/**
* Applies decorated descriptors
* @param target - Target object
* @param property - Property key being decorated
* @param decorators - Array of decorator functions
* @param descriptor - Original property descriptor
* @param context - Decorator context
* @returns Modified property descriptor
*/
function applyDecoratedDescriptor(
target: any,
property: PropertyKey,
decorators: Function[],
descriptor: PropertyDescriptor,
context: any
): PropertyDescriptor;Usage Example:
import _applyDecoratedDescriptor from '@babel/runtime/helpers/esm/applyDecoratedDescriptor';
// Used by Babel for legacy decorator syntax on methods
const descriptor = _applyDecoratedDescriptor(
MyClass.prototype,
'myMethod',
[logDecorator, validateDecorator],
Object.getOwnPropertyDescriptor(MyClass.prototype, 'myMethod'),
MyClass.prototype
);Defines properties with initializer functions for decorated fields.
/**
* Defines properties with initializers
* @param target - Target object
* @param key - Property key
* @param descriptor - Property descriptor with initializer
* @param context - Definition context
* @returns The target object
*/
function initializerDefineProperty(target: any, key: PropertyKey, descriptor: any, context?: any): any;Provides warnings for improper initializer usage in decorators.
/**
* Warnings for initializer usage
* @param descriptor - Descriptor being validated
* @param context - Usage context
* @throws Warning for improper initializer patterns
*/
function initializerWarningHelper(descriptor: any, context: any): void;// Current decorator context (2023-11)
interface DecoratorContext {
kind: 'class' | 'method' | 'getter' | 'setter' | 'field' | 'accessor';
name: string | symbol;
static: boolean;
private: boolean;
access: {
get?(object: any): any;
set?(object: any, value: any): void;
};
addInitializer(initializer: () => void): void;
}
// Class decorator context
interface ClassDecoratorContext extends DecoratorContext {
kind: 'class';
}
// Method decorator context
interface MethodDecoratorContext extends DecoratorContext {
kind: 'method';
}
// Field decorator context
interface FieldDecoratorContext extends DecoratorContext {
kind: 'field';
}
// Accessor decorator context
interface AccessorDecoratorContext extends DecoratorContext {
kind: 'accessor';
}
// Property descriptor interface
interface PropertyDescriptor {
configurable?: boolean;
enumerable?: boolean;
value?: any;
writable?: boolean;
get?(): any;
set?(value: any): void;
}
// Decorator function signatures
type ClassDecorator = (target: Function, context: ClassDecoratorContext) => Function | void;
type MethodDecorator = (target: Function, context: MethodDecoratorContext) => Function | void;
type FieldDecorator = (target: undefined, context: FieldDecoratorContext) => ((this: any, value: any) => any) | void;
type AccessorDecorator = (target: any, context: AccessorDecoratorContext) => any;
// Legacy decorator types
type LegacyClassDecorator = (target: Function) => Function | void;
type LegacyMethodDecorator = (target: any, propertyKey: PropertyKey, descriptor: PropertyDescriptor) => PropertyDescriptor | void;
type LegacyPropertyDecorator = (target: any, propertyKey: PropertyKey) => void;
// Decorator application result
interface DecoratorApplicationResult {
0: Function; // Enhanced class
1: Function[]; // Instance initializers
2: Function[]; // Static initializers
}The decorator helpers support the evolution of the decorator proposal:
applyDecsapplyDecs2203/applyDecs2203RapplyDecs2301applyDecs2305applyDecs2311 (Current)Migration Path:
// Legacy decorators (being phased out)
import _applyDecoratedDescriptor from '@babel/runtime/helpers/esm/applyDecoratedDescriptor';
// Current standard decorators
import _applyDecs2311 from '@babel/runtime/helpers/esm/applyDecs2311';