TypeScript Decorators
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);
}
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
24
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 8
Amazonbot 6
Ahrefs 2
Google 2
Unknown AI 2
Also referenced
How they use it
crawler 19
crawler_json 1
Related categories
⚡
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
🔍 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