Skip to content

Decorators

function log(target: any, key: string, desc: PropertyDescriptor) {
const orig = desc.value;
desc.value = function (...args: any[]) {
console.log(`call ${key}`, args);
return orig.apply(this, args);
};
}
class Svc {
@log
run(x: number) { return x * 2 }
}

Notes

  • Requires "experimentalDecorators": true prior to TS 5; standardized in TS 5.