Decorators are a powerful feature of TypeScript that allow you to modify or annotate classes, methods, properties, and parameters at design time. They provide a way to extend the functionality of existing code without modifying its structure. Decorators consist of an expression followed by an @
symbol, placed just before the declaration of the target element.
In this article, we will explore the concept of decorators and discuss some common use cases for utilizing them in your TypeScript projects.
Decorators in TypeScript are functions that are applied to a class, method, property, or parameter declaration using the @
symbol. They are a way to wrap or modify the behavior of the target element without directly modifying its implementation. Decorators share similarities with Python's decorators, Java annotations, and C#'s attributes.
Decorators can be applied to different elements such as classes, class methods, properties, or method parameters. Each decorator is defined as a separate function and can receive arguments or return values. Decorators are evaluated and applied in the order they appear in the codebase, from top to bottom.
To use a decorator, simply place the decorator's name prefixed with @
just before the declaration of the target element:
@decoratorName
class MyClass {
@decoratorName
myMethod() { }
@decoratorName
myProperty: string;
}
Decorators can be used to log or profile method calls or class usage. By applying a logging decorator to a method, you can easily track when and how often it is called, along with the provided arguments and returned values. This can be particularly useful for debugging and performance analysis.
function loggingDecorator(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log(`Calling ${propertyKey} with arguments:`, args);
const result = originalMethod.apply(this, args);
console.log(`Method ${propertyKey} returned:`, result);
return result;
};
return descriptor;
}
class MyClass {
@loggingDecorator
myMethod(arg1: string, arg2: number): string {
// Method implementation...
}
}
Decorators can be used to implement access control mechanisms for class members. By applying an access control decorator, you can restrict the access to certain properties or methods based on user roles or conditions. This provides a flexible way to enforce security policies.
function adminOnly(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
if (currentUser.role !== 'admin') {
throw new Error('Access denied!');
}
return originalMethod.apply(this, args);
};
return descriptor;
}
class MyRestrictedClass {
@adminOnly
myRestrictedMethod() {
// Method implementation...
}
}
Decorators can be used for input validation by ensuring that certain conditions are met before executing a method. By applying a validation decorator, you can automatically check the input parameters and prevent the method from running if the validation fails. This helps maintain data integrity and reliability in your codebase.
function validateParams(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
if (args.length === 0 || typeof args[0] !== 'string') {
throw new Error('Invalid parameter!');
}
return originalMethod.apply(this, args);
};
return descriptor;
}
class MyValidatorClass {
@validateParams
myValidatedMethod(name: string) {
// Method implementation...
}
}
These are just a few examples of how decorators can be used in TypeScript. Decorators provide a versatile and expressive way to enhance the functionality of your code, making it easier to maintain and extend over time.
Decorators in TypeScript allow you to modify or annotate class elements at design time, providing a way to extend their behavior without directly modifying their implementation. They are versatile and can be applied to classes, methods, properties, or parameters. By using decorators, you can enhance the functionality of your code, implement logging and profiling mechanisms, enforce access control, and perform input validation. Decorators are a powerful tool in the TypeScript ecosystem that helps you write cleaner, more flexible, and maintainable code.
noob to master © copyleft