Decorators
Prerequisites: Classes with types
A decorator attaches extra behavior to a class or its members,
written with an @ prefix — most commonly seen in frameworks like
Angular and NestJS.
function logCalls(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const original = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log(`Calling ${propertyKey}`);
return original.apply(this, args);
};
}
class Calculator {
@logCalls
add(a: number, b: number) {
return a + b;
}
}
Decorators require "experimentalDecorators": true in older
TypeScript configs (the syntax has since been standardized in
JavaScript itself) — check which version your framework expects.