TypeScript Tutorial

Decorators

Class, method, and property decorators (Standard & Experimental)

What are Decorators?

Decorators are a way to "annotate" and modify classes and their members at design time. They are a feature of ECMAScript (JavaScript) that TypeScript supported early on.
typescript
function Log(target: any, key: string) {
    console.log(`${key} was called`);
}

class User {
    @Log
    save() {
        // ... logic ...
    }
}

Two Versions of Decorators

Wait! This is a common source of confusion. There are two versions:
  1. Experimental Decorators: The original TypeScript version (requires "experimentalDecorators": true in tsconfig.json). Most older libraries (like Angular and TypeORM) use this.
  2. Standard Decorators: The new official ECMAScript standard version (TS 5.0+).

Types of Decorators

  • Class Decorators: Modify the entire class definition.
  • Method Decorators: Modify or replace a method definition.
  • Property Decorators: Observe when a property is declared.
  • Accessor Decorators: Modify getters or setters.

Decorator Factories

A Decorator Factory is a function that returns a decorator. This allows you to pass custom arguments to the decorator.
typescript
function Color(value: string) {
    return function (target: any) {
        target.prototype.color = value;
    };
}

@Color("red")
class Car {}

Summary

TermUsageDescription
@SyntaxHow you apply a decorator
experimentalConfigOld TS version (still widely used)
StandardTS 5.0+Official JS version of decorators
Factoryf(x) { return @d }Decorators with parameters
ReflectLibraryUsed with decorators for metadata reflection
Best ForLogging, Dependency Injection, Validation, Routing
Logic"Wrapping" existing code with extra behavior
OOPHighly popular in Object-Oriented patterns
SafetyDecorators run at RUNTIME; types only check them at COMPILE TIME
FrameworksAngular, NestJS, TypeORM, MobX
MetadataReflect.getMetadata (Extracting info from decorators)
ChainMultiple decorators can be applied to one item (Top to Bottom)