← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

TypeScript Decorators

typescript 4.0 Advanced

Also Known As

decorators @Injectable @Entity class decorator

TL;DR

Metadata annotations on classes, methods, and properties — used in NestJS, Angular, TypeORM, and class-validator to attach behaviour declaratively.

Explanation

TypeScript decorators (TC39 Stage 3 in 2024) annotate classes and members with @Decorator syntax. Class decorators modify or replace the class. Method decorators wrap methods. Property decorators define metadata on properties. Parameter decorators annotate constructor/method parameters. Decorator metadata: reflect-metadata stores type information accessed at runtime, enabling dependency injection (NestJS @Injectable), ORM mapping (TypeORM @Entity, @Column), and validation (class-validator @IsEmail). TypeScript 5.0 implements the TC39 decorator proposal; legacy decorators (experimentalDecorators) differ.

Common Misconception

TypeScript decorators are the same as Python decorators — Python decorators are functions wrapping functions at definition time; TypeScript decorators have a specific API and use reflect-metadata for rich type information.

Why It Matters

NestJS, Angular, and TypeORM depend on decorators for their core abstractions — understanding decorators explains how @Injectable creates services and how @Column maps properties to database columns.

Common Mistakes

  • Not enabling emitDecoratorMetadata and experimentalDecorators in tsconfig for legacy decorators.
  • Forgetting to import reflect-metadata at the entry point for metadata decorators.
  • Using legacy decorators (experimentalDecorators) with TS 5.0 new decorators — they are incompatible.
  • Decorators that have side effects during class definition — decorators run at class definition time, not instantiation.

Code Examples

✗ Vulnerable
// Manual DI wiring without decorators — verbose:
const userService = new UserService(new UserRepository(new Database()));
const orderService = new OrderService(userService, new OrderRepository(new Database()));
// Every constructor dependency must be manually wired
✓ Fixed
// NestJS decorators — declarative DI:
@Injectable()
export class UserService {
    constructor(private readonly userRepo: UserRepository) {}
}

@Entity('users')
export class User {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({ unique: true })
    @IsEmail()
    email: string;
}

@Controller('users')
export class UserController {
    constructor(private readonly userService: UserService) {}

    @Get(':id')
    findOne(@Param('id') id: number): Promise<User> {
        return this.userService.findById(id);
    }
}

Added 16 Mar 2026
Edited 22 Mar 2026
Views 24
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Perplexity 8 Amazonbot 6 Ahrefs 2 Google 2 Unknown AI 2
crawler 19 crawler_json 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
Enable experimentalDecorators: true in tsconfig for legacy decorators; note TypeScript 5.0 added standard Stage 3 decorators — the APIs differ between legacy and standard
📦 Applies To
typescript 4.0 web cli
🔗 Prerequisites
🔍 Detection Hints
Legacy @decorator syntax without experimentalDecorators; mixing legacy and TC39 standard decorators in same project
Auto-detectable: ✓ Yes typescript eslint
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: Class Tests: Update

✓ schema.org compliant