Chuck's Academy

Decorators and Namespaces in TypeScript

Property Decorators

Property decorators are used to modify or add metadata to class properties. A property decorator does not receive a descriptor of the property as in the case of method decorators. It receives two arguments: the prototype of the object and the name of the property.

Syntax of a Property Decorator:

typescript

In this example, the propertyDecorator decorator is applied to exampleProperty of the ExampleClass.

Practical Example:

Suppose we want to implement a decorator that logs a message every time a specific property is accessed.

typescript

In this example, the logPropertyAccess decorator ensures that every time exampleProperty is accessed or assigned a value, a message is logged to the console.


Ask me anything