Decorators provide a declarative syntax to annotate and modify classes, methods, accessors, and fields. TypeScript supports modern TC39 Stage 3 decorators (TS 5.0+) and experimental legacy decorators.
// Modern Method Decorator (TS 5.0+)
function AuditLog(originalMethod: any, context: ClassMethodDecoratorContext) {
const methodName = String(context.name);
return function (this: any, ...args: any[]) {
console.log(`[AUDIT LOG] Executing ${methodName} at ${new Date().toISOString()}`);
const result = originalMethod.apply(this, args);
console.log(`[AUDIT LOG] Completed ${methodName} cleanly.`);
return result;
};
}
class PaymentProcessor {
@AuditLog
public processPayment(accountNumber: string, amount: number): boolean {
console.log(`Processing \$${amount} for account ${accountNumber}...`);
return true;
}
}
const processor = new PaymentProcessor();
processor.processPayment("ACC-99182", 450.00);
"experimentalDecorators" when starting new projects unless required by legacy frameworks like NestJS or TypeORM.ClassMethodDecoratorContext and ClassFieldDecoratorContext.What decorator flag is configured in tsconfig.json for legacy Angular / NestJS decorator support? (Answer: "experimentalDecorators": true)
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With