{
    "slug": "typescript_decorators",
    "term": "TypeScript Decorators",
    "category": "typescript",
    "difficulty": "advanced",
    "short": "Metadata annotations on classes, methods, and properties — used in NestJS, Angular, TypeORM, and class-validator to attach behaviour declaratively.",
    "long": "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.",
    "aliases": [
        "decorators",
        "@Injectable",
        "@Entity",
        "class decorator"
    ],
    "tags": [
        "typescript",
        "oop",
        "frameworks"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "typescript_generics",
        "dependency_injection",
        "typescript_interfaces_types"
    ],
    "prerequisites": [
        "typescript_config",
        "typescript_interfaces_types",
        "php_attributes_builtin"
    ],
    "refs": [
        "https://www.typescriptlang.org/docs/handbook/decorators.html"
    ],
    "bad_code": "// Manual DI wiring without decorators — verbose:\nconst userService = new UserService(new UserRepository(new Database()));\nconst orderService = new OrderService(userService, new OrderRepository(new Database()));\n// Every constructor dependency must be manually wired",
    "good_code": "// NestJS decorators — declarative DI:\n@Injectable()\nexport class UserService {\n    constructor(private readonly userRepo: UserRepository) {}\n}\n\n@Entity('users')\nexport class User {\n    @PrimaryGeneratedColumn()\n    id: number;\n\n    @Column({ unique: true })\n    @IsEmail()\n    email: string;\n}\n\n@Controller('users')\nexport class UserController {\n    constructor(private readonly userService: UserService) {}\n\n    @Get(':id')\n    findOne(@Param('id') id: number): Promise<User> {\n        return this.userService.findById(id);\n    }\n}",
    "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",
    "severity": "low",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/typescript_decorators",
        "html_url": "https://codeclaritylab.com/glossary/typescript_decorators",
        "json_url": "https://codeclaritylab.com/glossary/typescript_decorators.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[TypeScript Decorators](https://codeclaritylab.com/glossary/typescript_decorators) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/typescript_decorators"
            }
        }
    }
}