Decorators and metadata
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Distinguish legacy and standard decorators.
- Type a method decorator.
- Understand what metadata costs.
Overview
#Two flavors
Legacy decorators (experimentalDecorators) are the older proposal used by Angular and TypeORM. Standard decorators (TypeScript 5.0+) follow the TC39 proposal and have a different signature.
A standard method decorator
function logged<This, Args extends unknown[], R>(
value: (this: This, ...args: Args) => R,
context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => R>,
) {
return function (this: This, ...args: Args): R {
console.log(context.name);
return value.apply(this, args);
};
}
The context object carries the name, kind and accessors.
Legacy decorator
function sealed(target: Function) {
Object.seal(target);
}
@sealed
class Service {}
Legacy decorators receive the target directly and need experimentalDecorators.
Metadata
emitDecoratorMetadata emits design-time type metadata for reflection, which increases bundle size and couples runtime behavior to types. Prefer explicit metadata or a schema when possible.
Advice
Use standard decorators for new code. Keep decorator logic small; it runs at class definition time and is hard to debug.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
When does a class decorator run?
Exercise 2Implement
Write a standard method decorator that logs the method name before calling the original.
starter.typescriptexport function logged<This, Args extends unknown[], R>( value: (this: This, ...args: Args) => R, context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => R>, ) { // your code }Reveal the reference answer
reference.typescriptexport function logged<This, Args extends unknown[], R>( value: (this: This, ...args: Args) => R, context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => R>, ) { return function (this: This, ...args: Args): R { console.log(context.name); return value.apply(this, args); }; }Authored expected output: Compiles with no errors. Runtime: calling the method logs its name first.
The decorator returns a replacement method that wraps the original.
Exercise 3Explain
What is the cost of `emitDecoratorMetadata`, and when is it justified?
Reveal the reference answer
reference.typescriptIt emits extra runtime metadata that describes the design-time types of decorated members, which increases bundle size and makes runtime behavior depend on type information that is otherwise erased. It is justified only when a framework needs reflection, such as dependency injection that resolves constructor parameter types, and even then a lighter explicit registration is often preferable.Authored expected output: A written answer naming bundle size and the coupling of runtime reflection to types.
Metadata trades bundle size and clarity for runtime reflection.
Common misconceptions
#- Decorators run per instance.
- Standard and legacy decorators are interchangeable.
- Decorators are erased like types.
Follow-up practice
#- typescript-js-to-ts-migration
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.