Legacy decorators and metadata
experimentalDecorators and emitDecoratorMetadata: how Angular and NestJS do dependency injection. A free TypeScript lesson with examples.
More on Decorators
Keep learning
Overview
chapters · decorators Legacy decorators and metadata Angular and NestJS still run on the older, TypeScript-only decorators. Learn their signatures and how type metadata (type info kept in the JavaScript) powers dependency injection (the framework creates the objects your class needs and passes them in).
1. The problem: NestJS code in a fresh TS 5 project
Copy a NestJS service into a new project. Without one flag, every decorator is wrong. no flag: standard decorators Decorators are not valid here. Unable to resolve signature of method decorator when called as an expression. The runtime will invoke the decorator with 2 arguments, but the decorator expects 3. Line 4: standard decorators cannot sit on a parameter at all. Line 7: Nest's Log() expects the legacy 3 arguments, but standard mode calls it with 2: (value, context). tsconfig for legacy mode experimentalDecorators switches to the legacy decorator rules. emitDecoratorMetadata writes type info into the JavaScript (section 3). Same source now compiles. Two modes, two different APIs: you pick one per project.
2. Legacy signatures: (target, key, …)
target is the prototype (the shared object holding instance methods) for instance members, and the class itself for static ones. key is the member's name. A descriptor is an object that describes a property: its value, whether it is writable, and so on. decorator on @d class OrderService (constructor) → new constructor | void (target, key, descriptor) → descriptor | void (target, key) → void no initial value, no descriptor constructor(@d db: Db) (target, key, index) → void parameter: legacy only
@logged, legacy style: swap descriptor.value
Line 3 replaces the method stored in the descriptor with a wrapper. Nothing is returned, so the edited descriptor is used.
a parameter decorator only records a fact
Reflect.getOwnMetadata and Reflect.defineMetadata come from the reflect-metadata library: they read and store extra data on a class. ?? [] means "or an empty list if nothing is stored yet". index is the parameter's position (0, 1, …).
3. emitDecoratorMetadata: types survive into JavaScript
Normally types vanish. With this flag, TypeScript writes some of them into the output, for decorated classes only. you write tsc emits (simplified) __decorate, __param and __metadata are small helper functions tsc writes into the output. [Db, Mailer] are the real classes, readable at runtime with the reflect-metadata library. metadata key [Db, Mailer] constructor or method parameter types Function / String / … type of a property, or Function for a method String / undefined
container.ts (needs import 'reflect-metadata')
import type is deleted from the JavaScript, so the class Db is not available at runtime. TypeScript cannot write Db into the metadata and writes Function instead. The container then cannot build a Db. esbuild / Vite
6. Legacy vs standard
standard (TS 5.0+) flag needed call shape (target, key, descriptor) (value, context) parameter decorators type metadata (design:*) accessor keyword field initial value JavaScript standard (TC39)
7. Migrating
Framework decides Angular, NestJS, TypeORM, class-validator need legacy mode today. Keep the flag. Own decorators Property decorators become field decorators returning an initializer, or accessor decorators. One mode per project the flag switches every decorator in the program. You cannot mix the two.
Remember
experimentalDecorators switches the whole project to the legacy API. emitDecoratorMetadata writes design:paramtypes for decorated classes. DI containers read those types to build constructor arguments. New code without a framework: use standard decorators. ! Cheat code Angular/Nest → both flags on, plus reflect-metadata. Inject interfaces with tokens, never by type. Everything else → standard decorators, no flags. → Go deeper: standard decorators, classes in TypeScript, import type and verbatimModuleSyntax, tsconfig flags.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.