TypeScript 语言 进阶技巧 装饰器的高级用法

TypeScript阿木 发布于 2025-05-28 12 次阅读


TypeScript 装饰器的高级用法:深入探索与实战

TypeScript 作为 JavaScript 的超集,提供了丰富的类型系统和装饰器等高级特性。装饰器是 TypeScript 中一种强大的功能,它允许开发者在不修改原有代码结构的情况下,对类、方法、属性等进行扩展和增强。本文将深入探讨 TypeScript 装饰器的高级用法,包括类装饰器、方法装饰器、属性装饰器以及装饰器组合等,并通过实际案例展示如何利用装饰器提升代码的可读性和可维护性。

类装饰器

类装饰器是应用于类声明上的装饰器,它可以接收一个参数,即被装饰的类的构造函数。类装饰器可以用来修改类的行为,例如添加新的属性、方法或修改现有属性和方法。

示例:添加类属性

typescript
function ClassDecorator(target: Function) {
Object.defineProperty(target, 'classProperty', {
value: 'This is a class property added by decorator',
writable: true,
configurable: true,
enumerable: true
});
}

@ClassDecorator
class MyClass {
constructor() {
console.log(this.classProperty); // 输出: This is a class property added by decorator
}
}

示例:修改构造函数

typescript
function ConstructorDecorator(target: Function) {
target.prototype.constructor = function() {
console.log('Modified constructor');
};
}

@ConstructorDecorator
class MyClass {
constructor() {
console.log('Original constructor');
}
}

方法装饰器

方法装饰器是应用于类的方法上的装饰器,它可以接收三个参数:装饰的目标方法、目标方法的属性描述符以及目标方法的属性名。

示例:添加方法前缀

typescript
function MethodDecorator(target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
descriptor.value = function() {
console.log('Method called with prefix');
return descriptor.value.apply(this, arguments);
};
}

class MyClass {
@MethodDecorator
public myMethod() {
console.log('This is my method');
}
}

示例:方法重载

typescript
function MethodOverloadDecorator(target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = function(...args: any[]) {
if (args.length === 1) {
return originalMethod.call(this, args[0]);
} else {
return originalMethod.call(this, args[0], args[1]);
}
};
}

class MyClass {
@MethodOverloadDecorator
public myMethod(a: number, b?: number) {
if (b) {
return a + b;
} else {
return a 2;
}
}
}

属性装饰器

属性装饰器是应用于类属性上的装饰器,它可以接收两个参数:装饰的目标属性和属性描述符。

示例:添加属性验证

typescript
function PropertyValidator(target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
const originalValue = descriptor.value;
descriptor.value = function(newValue: any) {
if (typeof newValue !== 'number') {
throw new Error('Property must be a number');
}
return originalValue.call(this, newValue);
};
}

class MyClass {
@PropertyValidator
public myProperty: number;

constructor() {
this.myProperty = 10;
}
}

装饰器组合

装饰器可以组合使用,即一个装饰器可以应用于另一个装饰器。这种组合可以创建出更复杂的装饰器效果。

示例:组合装饰器

typescript
function MethodLogger(target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
console.log(`Method ${propertyKey} called`);
}

function MethodOverloadDecorator(target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = function(...args: any[]) {
if (args.length === 1) {
return originalMethod.call(this, args[0]);
} else {
return originalMethod.call(this, args[0], args[1]);
}
};
}

class MyClass {
@MethodLogger
@MethodOverloadDecorator
public myMethod(a: number, b?: number) {
if (b) {
return a + b;
} else {
return a 2;
}
}
}

总结

TypeScript 装饰器是一种强大的工具,它可以帮助开发者在不修改原有代码的情况下,对类、方法、属性等进行扩展和增强。我们学习了类装饰器、方法装饰器、属性装饰器以及装饰器组合的高级用法。在实际开发中,合理运用装饰器可以提高代码的可读性和可维护性,使代码更加灵活和强大。

(注:本文仅为示例性介绍,实际应用中装饰器的使用应根据具体需求进行调整。)