TypeScript - Decorators (TS 5.x)

Standard decorators

TS 5.x supports the new standard decorators (different from legacy experimental decorators). Enable with "experimentalDecorators": true or follow current TS guidance as features stabilize.

function log(target: any, context: ClassMethodDecoratorContext) {
  const name = String(context.name);
  return function(this: any, ...args: any[]) {
    console.log(`Calling ${name} with`, args);
    return target.apply(this, args);
  }
}

class Greeter {
  @log
  say(msg: string) { return `Hello ${msg}` }
}

Note: The exact semantics differ from legacy decorators. Verify your tsconfig and transformer support in your toolchain.