Chapter 35: TypeScript Decorators

TypeScript Decorators very calmly, step by step, like we’re sitting together with VS Code open, slowly typing examples, and really understanding what they are, why they exist, how they changed in TypeScript 5.0, and what realistic patterns people actually use in 2025–2026.

Decorators are one of the most loved (and sometimes most controversial) features in modern TypeScript — especially in frameworks like NestJS, Angular, tRPC procedures, class-based React components, MobX, Zod-like schema builders, etc.

1. What is a decorator? (the simplest honest explanation)

A decorator is a special kind of function that you place above a class, method, property, accessor (getter/setter), or parameter — using the @ symbol.

It receives information about what it’s decorating (the target, name, descriptor, etc.) and can:

  • modify the behavior
  • add metadata
  • replace the thing entirely
  • log / track usage
  • enforce rules at design time

Think of it like annotations in Java / Python / C#, but more powerful because JavaScript is dynamic.

2. Very important history — two eras of decorators

Era Syntax / Proposal stage Supported in TS version Status in Feb 2026 Most code you see today?
Legacy / experimental @decorator TS 1.5 – 4.9 Still works (with experimentalDecorators) Old AngularJS / old NestJS projects
Modern / stage-3 @decorator TS 5.0+ (stable) Recommended — aligned with TC39 proposal Almost all new code in 2026

In February 2026you should almost always use the modern stage-3 decorators (unless maintaining very old code).

3. Modern decorator syntax (TS 5.0+ — this is what you should learn)

TypeScript

4. The five places you can put decorators (2026 reality)

Location Decorator context type Most common real use-case in 2026
Class ClassDecoratorContext Dependency injection (NestJS @Injectable()), MobX
Method ClassMethodDecoratorContext Logging, timing, authorization, caching
Property ClassFieldDecoratorContext Auto-observable (MobX), validation metadata
Getter / Setter ClassGetterDecoratorContext / Setter Lazy loading, memoization
Parameter ClassParameterDecoratorContext Dependency injection (NestJS @Body(), @Query())

5. Realistic, production-grade examples (what you actually see in 2026)

Example 1: Simple timing decorator (very common utility)

TypeScript

Example 2: Authorization / role guard (very frequent in NestJS-like code)

TypeScript

Example 3: Property decorator — auto-validation metadata (Zod-like pattern)

TypeScript

6. Quick cheat-sheet — decorator kinds & context

Decorator on Context type this inside decorator function Most common libraries using it
class ClassDecoratorContext undefined @Injectable(), @Controller()
method ClassMethodDecoratorContext the instance logging, auth, caching
field/property ClassFieldDecoratorContext undefined MobX @observable, validation
getter / setter ClassGetterDecoratorContext undefined memoization, lazy
parameter ClassParameterDecoratorContext undefined DI — @Inject, @Body()

7. tsconfig.json settings needed in 2026

JSON

Note: experimentalDecorators = legacy mode — you should not enable it for new code.

8. Summary — 2026 honest verdict

Scenario Should you use decorators? Recommended style
New NestJS / Angular / tRPC project Yes — heavily Modern stage-3 decorators
Plain React function components Rarely Prefer hooks / HOCs
Small script / utility No Plain functions
Maintaining old AngularJS / legacy Yes (legacy mode) Keep experimentalDecorators: true
Learning TypeScript Yes — understand both Focus on modern syntax first

Want to go deeper into one area?

  • Full NestJS controller + service decorator example
  • How MobX / class-transformer / class-validator use decorators
  • Writing a real metadata-based validation system
  • Differences between legacy & stage-3 decorators (emitted JS)
  • Common gotchas & performance considerations

Just tell me — we’ll zoom right in there! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *