Декораторы классов
typescript
1function Log(constructor: Function) {2 console.log('Created:', constructor.name);3}45@Log6class MyClass {}
Декораторы методов
@Log decorator, логирующий вызовы методов:
typescript
1function Log(target: any, key: string, descriptor: PropertyDescriptor) {2 const original = descriptor.value;3 descriptor.value = function(...args: any[]) {4 console.log(`Calling ${key} with`, args);5 return original.apply(this, args);6 };7}
Заключение
Декораторы расширяют возможности TypeScript.
Изучите вопросы по TypeScript в нашем разделе.