# Modern Decorators

> Intercepting and wrapping methods.

Subject: TypeScript | Lesson 45 of 54 | 5 min read
Interactive lesson: https://codewithanimation.in/typescript/decorators
Previous: [Abstract Classes & Methods](https://codewithanimation.in/typescript/abstract-classes-methods.md) | Next: [Modules vs Namespaces](https://codewithanimation.in/typescript/modules-vs-namespaces.md)

## Declarative Wrappers

A decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter.

In TypeScript 5.0+, decorators are standard (not experimental). They are simply functions that intercept the target they are attached to, allowing you to wrap or modify it.

## Cross-Cutting Concerns

Imagine you want to log the execution time of 50 different methods across your app.

Instead of writing `console.time()` inside every single method, you can write one `@time` decorator and stick it on top of any method you want!

## Syntax (TS 5.0+)

```typescript
// The decorator function
function logged(originalMethod: any, context: ClassMethodDecoratorContext) {
  return function (this: any, ...args: any[]) {
    console.log(`Calling ${String(context.name)} with args:`, args);
    return originalMethod.call(this, ...args);
  };
}

class Calculator {
  @logged
  add(a: number, b: number) {
    return a + b;
  }
}

const calc = new Calculator();
calc.add(2, 3); // Logs: "Calling add with args: [2, 3]"
```

## Common mistake: The Legacy Rift

For years, TypeScript supported "experimental" decorators (enabled via `experimentalDecorators` in tsconfig). These were used heavily in frameworks like Angular and NestJS.

The new ECMAScript standard decorators (supported in TS 5.0+) have a *completely different API signature*. You cannot mix and match them easily.

## Tip: The Context Object

Modern decorators receive a `context` object as their second argument. This object contains metadata about what is being decorated (its name, type, whether it's static, etc.).

## Try it: Try it

Watch how a `@logged` decorator wraps a class method, intercepting the call to inject its own logic before running the original method.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/decorators

## Check yourself

### Question 1

In modern TS 5.0 decorators, what does a method decorator function return?

- A. A boolean indicating if the method is allowed to run.
- B. A replacement function that wraps or replaces the original method.
- C. A new class instance.

Correct answer: B

A method decorator receives the original method and returns a replacement method. When the method is called, your replacement function fires instead, giving you a chance to run logic before or after the original function.

## Remember this

- Decorators use the `@` syntax.
- TS 5.0 standard decorators are different from the old experimental ones.
- Method decorators return a replacement function to wrap the original.
- They are perfect for logging, timing, auth checks, and memoization.
