Lesson 20 of 56
TypescriptDecorators (Experimental) – Class & Method
20 minutes
Course progress36%
Enabling Decorators
Add to tsconfig.json:
"experimentalDecorators": true,
"emitDecoratorMetadata": true
Lesson 20 of 56
Typescript20 minutes
Add to tsconfig.json:
"experimentalDecorators": true,
"emitDecoratorMetadata": true
function sealed(constructor: Function) {
Object.seal(constructor);
Object.seal(constructor.prototype);
}
@sealed
class Service {
// …
}
function log(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const original = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log(`Calling ${propertyKey} with`, args);
return original.apply(this, args);
};
}
class MathOps {
@log
add(a: number, b: number) {
return a + b;
}
}