TypeScript · Types that pay for themselves

How do decorators work in TypeScript?

HarddecoratorsmetadataclassesUpdated Oct 2026
Quiz this track

Short answer

Decorators are functions that annotate or modify classes, methods, accessors, fields or parameters. Modern TypeScript supports standard ECMAScript decorators, while legacy experimental decorators differ in semantics and configuration.

A decorator runs when the decorated declaration is evaluated. It receives the declaration and a context object, and can return a replacement. Standard decorators are the ECMAScript proposal supported by TypeScript 5 and later. Legacy decorators, enabled with experimentalDecorators, use different signatures and are common in older frameworks.

ts
function logged<This, Args extends unknown[], Return>(
  method: (this: This, ...args: Args) => Return,
  context: ClassMethodDecoratorContext<
    This,
    (this: This, ...args: Args) => Return
  >,
) {
  return function (this: This, ...args: Args): Return {
    console.log("calling " + String(context.name));
    return method.apply(this, args);
  };
}

class Service {
  @logged
  run(task: string) {
    return "done: " + task;
  }
}

Standard versus legacy

  • Standard decorators receive a context with kind, name, addInitializer and metadata
  • Legacy decorators receive different arguments and often rely on emitDecoratorMetadata
  • experimentalDecorators changes the signatures and cannot be combined casually with standard decorators
  • Frameworks such as Angular and older NestJS versions rely on legacy decorator behavior
  • Decorator metadata is emitted only when the relevant compiler options are enabled

Decorators are useful for cross-cutting concerns such as logging, dependency injection and validation. They are less useful for core business logic because they hide the call flow from a reader and can make debugging harder. Use them where a framework or a clearly repeated concern justifies them.

Check the framework's supported decorator mode before upgrading TypeScript. Mixing standard and legacy decorator semantics is a common source of runtime surprises.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the difference between standard and legacy decorators?
  2. 2What does a method decorator receive?
  3. 3When is emitDecoratorMetadata needed?
  4. 4Why can decorators make debugging harder?

Say it in the interview

Decorators are functions that can observe or replace classes, methods, fields and accessors. Modern TypeScript implements the standard decorator proposal, while older code may use experimentalDecorators with different signatures and metadata behavior. I use them when a framework expects them, such as dependency injection or ORM mapping. I avoid building core application logic around them because they hide control flow.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live TypeScript mock interview and get scored feedback on depth, clarity and confidence.