Skip to content

Legacy Decorators

Goal

Create a legacy method decorator using the unified context style.

Implementation

ts
import { Methods } from '@litert/decorator/legacy';

function track(label: string): Methods.ICallbackFn {

    return Methods.create((ctx) => {
        Reflect.defineProperty(ctx.constructor, label, {
            configurable: true,
            value: ctx.name,
        });
    });
}

class Service {

    @track('trackedMethod')
    public run(): void {}
}

Metadata

Legacy decorators should use the reflect-metadata module to store and read metadata. Do not use getMetadataContainer — that API is designed for modern (Stage 3) decorators only.

ts
import 'reflect-metadata';
import { Methods } from '@litert/decorator/legacy';

function mark(name: string): Methods.ICallbackFn {

    return Methods.create((ctx) => {
        Reflect.defineMetadata(name, true, ctx.constructor);
    });
}

class Service {

    @mark('tracked')
    public run(): void {}
}

console.log(Reflect.getMetadata('tracked', Service)); // true

Example code

Decorator kindExample
ClassClass.ts
MethodMethod.ts
PropertyProperty.ts
AccessorAccessor.ts
GetterGetter.ts
SetterSetter.ts
Static methodStaticMethod.ts
Static propertyStaticProperty.ts
Static accessorStaticAccessor.ts
Static getterStaticGetter.ts
Static setterStaticSetter.ts
Method parameterMethodParameter.ts
Static method parameterStaticMethodParameter.ts
Constructor parameterConstructorParameter.ts

Notes

StepReason
Use Methods.createThe callback receives one normalized context object instead of raw legacy decorator arguments.
Read ctx.constructorThe unified context always includes the decorated class constructor.
Read ctx.descriptor when availableMethod, accessor, getter, and setter decorators expose their property descriptor through the context.