Decorators
suggest changeTypeScript supports the current TC39 decorators proposal. A decorator receives the decorated value and a context object. The older model enabled by experimentalDecorators has different signatures and emit behavior; examples written for one model generally cannot be used unchanged with the other. Proposal-style decorators are also incompatible with emitDecoratorMetadata and cannot decorate parameters.
function register<T extends abstract new (...args: never[]) => object>(
target: T,
context: ClassDecoratorContext<T>,
): void {
console.log(`registered ${String(context.name)}`);
void target;
}
@register
class Service {
execute(): string {
return "done";
}
}
Decorators run when a class is defined, not when it is instantiated. A class decorator can observe, replace, or register a class, but any replacement must preserve the expected constructor behavior. Keep side effects explicit, and use a normal function when it communicates the operation more clearly.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents