Decorators
What a decorator is
A decorator is a function that runs when a class or member is defined. The at sign attaches it, and the function observes or wraps the target.
function sealed(target: any, context: any): void { Object.seal(target); }
Class decorators
A class decorator runs once for the whole class. Sealing below locks the constructor so new static members cannot be added later.
function sealed(target: any, context: any): void { Object.seal(target); } @sealed class User { name: string = "Ada"; } console.log(Object.isSealed(User));
true
Method decorators
A method decorator runs once for each decorated method. Logging the method name at setup time helps trace which methods exist.
function log(target: any, context: any): void { console.log("Registering " + String(context.name)); } class Service { @log save(id: string): void { console.log(id); } } new Service().save("42");
Registering save
42
The first line comes from the decorator when the class is defined; the second comes from the call.
experimentalDecorators note + TS 5 standard note
TypeScript 5 ships the standard TC39 decorator shape with no extra flag. The two argument shape above, target plus context, matches that standard.
The older experimentalDecorators flag enables the legacy proposal and changes the call shape, so leave it off for new code.
{ "compilerOptions": { "target": "ES2022" } }
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
When does a class decorator run?
Answer
Once, when the class is defined
The decorator runs at definition time and can observe or replace the class once.
-
What two arguments does a standard decorator receive?
Answer
The target and a context object
The standard shape passes the decorated target and a context that names it and its kind.
-
Does standard decorator syntax in TypeScript 5 need the
experimentalDecoratorsflag?Answer
No, the standard shape works without a flag
TypeScript 5 ships the TC39 decorators, and the flag belongs to the older proposal.
-
What does turning on
experimentalDecoratorsselect?Answer
The legacy proposal, with a different call shape
The flag keeps the old proposal alive, and its decorators are called with different arguments.
-
What does this program print?
function sealed(target: any, context: any): void { Object.seal(target); } @sealed class User { name: string = "Ada"; } console.log(Object.isSealed(User));Answer
true
The decorator seals the class when it is defined, so the check reports true.
-
What does this program print?
function log(target: any, context: any): void { console.log("Registering " + String(context.name)); } class Service { @log save(id: string): void { console.log(id); } } new Service().save("42");Answer
Registering save / 42
The decorator logs the method name when the class is defined, and the call logs the id.
-
What does this program print?
function named(target: any, context: any): void { console.log(context.name); } @named class User {} console.log("ready");Answer
User / ready
The class decorator runs at definition time, before the line that logs ready.
-
What does this program print?
function log(target: any, context: any): void { console.log(context.name); } class Service { @log save(): void {} @log load(): void {} } console.log("done");Answer
save / load / done
Each method decorator runs in source order when the class is defined, and done comes last.
-
What does the compiler say about this program?
function legacy(target: any, key: string, descriptor: PropertyDescriptor): void {} class Service { @legacy save(): void {} } console.log(new Service());Answer
error TS1241: Unable to resolve signature of method decorator when called as an expression.
A standard method decorator receives two arguments, so a three-argument signature does not fit.
-
What does the compiler say about this program?
function log(target: any, context: any): void {} class Service { @log() save(): void {} } console.log(new Service());Answer
error TS2554: Expected 2 arguments, but got 0.
The parentheses call the decorator with no arguments, while the at sign expects the function itself.
Article author: Arthur Isaev