Decorators

TypeScript decorators provide a powerful system for annotating class definitions to observe and modify their functionality.

A decorator definition is a special function used to annotate some aspect of the class definition. By inserting the function name, preceded by an @ character, the function is called when the class is loaded. You can use decorators to gain full access to the initialization logic, object properties, instance methods, and even method parameters of a class definition.

Loading TypeScript...

Class decorators

With decorator functions, you can take complete control over how a class declaration works. Notice in the example below that the classDecorator function is called before any instances of the class are created.

Loading TypeScript...

A common pattern for using decorators is to provide a decorator factory function, which produces a function whose type matches the global utility type ClassDecorator.

Loading TypeScript...

Method decorators

A method decorator is used to annotate a method declaration.

Loading TypeScript...

Was this page helpful?