Chuck's Academy

Basic TypeScript

Decorators in TypeScript

Decorators in TypeScript are a powerful feature that allows you to modify the behavior of classes, methods, properties, and parameters in a flexible and reusable way. This feature is inspired by languages like Python and is widely used in frameworks like Angular.

This image shows a logo of TypeScript decoratorsThis image shows a logo of TypeScript decorators

Introduction to Decorators

A decorator is a function that applies to a class, method, property, or parameter and can modify its behavior. To use decorators, you need to enable this feature in the tsconfig.json file:

json
"In this tsconfig.json file, we enable the experimentalDecorators option so we can use decorators in TypeScript."

Syntax of a Decorator

A decorator is simply a function that receives the element it is decorating as an argument. Let's look at a basic example:

typescript
"In this example, we define a decorator called log that applies to the add method of the Calculator class. Each time add is called, the decorator logs a message to the console with the arguments."

Types of Decorators

Class Decorator

A class decorator applies to the definition of a class and can modify its behavior. The decorator receives the class constructor as an argument.

typescript
"Here, the sealed decorator applies the Object.seal function to the constructor of the Person class, preventing properties from being added or removed from the class or its prototype."

Method Decorator

Method decorators apply to class methods. They can modify the behavior of methods, as we saw in the previous example.

Property Decorator

A property decorator is used to modify the behavior of a property. This decorator cannot access the property's value; it can only modify its configuration.

typescript
"In this example, the readonly decorator applies to the brand property of the Car class, making the property read-only."

Parameter Decorator

A parameter decorator applies to a method's parameters and can be used to inspect or modify the parameter values.

typescript
"This example shows a parameter decorator called logParameter that logs the index of the parameter it is applied to in the greet method."

Decorators in Frameworks

Decorators are widely used in frameworks like Angular, where they are used to define components, services, and other parts of the application. For example, in Angular, the @Component decorator is used to define a component:

typescript
"This example shows how the @Component decorator is used in Angular to define a component. The decorator defines the selector, template, and styles associated with the component."

Conclusion

In this chapter, we have learned what decorators are in TypeScript and how to use them to modify the behavior of classes, methods, properties, and parameters. Decorators are a powerful tool widely used in frameworks like Angular to structure complex applications in a clean and efficient manner.


Ask me anything