Decorator Pattern Implementation in TypeScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Decorator Pattern runs in O(1) time and O(n) space, in TypeScript as in every other language.
- ✓TypeScript has TWO decorator systems and they are not compatible. The legacy system, enabled by experimentalDecorators, is what Angular, NestJS, TypeORM and class-validator are built on; its method decorator receives (target, propertyKey, descriptor).
- ✓Pick one system per project and state which in the tsconfig, because mixing them is not possible and the error messages when you try are unhelpful.
- ✓The TypeScript trap: The two systems have DIFFERENT SIGNATURES, so a decorator copied from a Stage-3 blog post silently fails to compile, or worse, compiles with implicit any, inside an Angular project, and vice versa.
- ✓Decorators run once per class definition at module evaluation, so their own cost is startup-only.
What Decorator Pattern actually is
Decorator Pattern in TypeScript: what you actually reach for
TypeScript implementation
// STANDARD TC39 decorators (TypeScript 5.0+, no experimentalDecorators).
function logged<T, A extends unknown[], R>(
target: (this: T, ...args: A) => R,
context: ClassMethodDecoratorContext<T>,
): (this: T, ...args: A) => R {
const name = String(context.name);
return function (this: T, ...args: A): R {
console.log('->', name);
return target.call(this, ...args);
};
}
class Service {
@logged
fetch(id: number): string { return 'row ' + id; }
}
// LEGACY decorators (experimentalDecorators: true) have a DIFFERENT shape.
// This does NOT compile under the standard system, and vice versa:
//
// function legacyLogged(target: any, key: string, desc: PropertyDescriptor) {
// const original = desc.value;
// desc.value = function (...args: any[]) { return original.apply(this, args); };
// }
//
// Angular / NestJS / TypeORM require the LEGACY system. Choose one per
// project, the two cannot coexist in a single compilation.
export { Service };Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe TypeScript-specific trap
Performance in TypeScript, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which TypeScript type or library should I use for Decorator Pattern?
TypeScript has TWO decorator systems and they are not compatible. The legacy system, enabled by experimentalDecorators, is what Angular, NestJS, TypeORM and class-validator are built on; its method decorator receives (target, propertyKey, descriptor). The standard TC39 system, available without the flag since TypeScript 5.0, receives (value, context) where context carries kind, name, addInitializer and access. Turning experimentalDecorators on selects the legacy system for the whole compilation.
What is the most common TypeScript mistake when implementing Decorator Pattern?
The two systems have DIFFERENT SIGNATURES, so a decorator copied from a Stage-3 blog post silently fails to compile, or worse, compiles with implicit any, inside an Angular project, and vice versa. The second trap is that legacy parameter decorators have no standard equivalent at all, so dependency-injection frameworks that rely on them cannot migrate. Third, emitDecoratorMetadata (which TypeORM and NestJS depend on for reflected types) works ONLY with the legacy system and only for a narrow set of type shapes; a union or an interface emits Object and the framework silently misbehaves.
How does Decorator Pattern actually perform in TypeScript?
Decorators run once per class definition at module evaluation, so their own cost is startup-only. What is not free is what they install: a method decorator that replaces the method adds a call frame to EVERY invocation forever. emitDecoratorMetadata additionally emits Reflect.metadata calls and pulls in the reflect-metadata polyfill, adding both bundle size and per-class startup work. Time complexity is O(1) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what Replit offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.