Decorator (TypeScript)

A decorator is a special declaration prefixed with `@` that attaches extra behavior or metadata to a class, method, or property. It is widely used by frameworks like Angular and NestJS.

TypeScript

A decorator is a function that runs when the class is defined and can observe or modify the thing it's attached to. Frameworks use them for routing, dependency injection, validation, and more, keeping that wiring declarative and close to the code.

Decorators were long an experimental TypeScript feature; the modern standardized version is supported in recent releases. Either way they emit real JavaScript rather than being erased like most types.

Syntax

@decorator
class Name { ... }

Examples

TypeScript

function sealed(target: Function) {
  Object.seal(target);
  Object.seal(target.prototype);
}

@sealed
class Greeter {
  greet() { return "hi"; }
}

console.log(new Greeter().greet());

Output:

hi

Learn it in a lesson

See also

Back to the Dictionary