37 lines
937 B
TypeScript
37 lines
937 B
TypeScript
// Test file to verify decorator functionality
|
|
const decoratedClasses: Function[] = [];
|
|
|
|
function trackClass(constructor: Function) {
|
|
decoratedClasses.push(constructor);
|
|
return constructor;
|
|
}
|
|
|
|
function logMethod(_target: any, context: ClassMethodDecoratorContext) {
|
|
const methodName = String(context.name);
|
|
return function (this: any, ...args: any[]) {
|
|
console.log(`Calling method: ${methodName}`);
|
|
return (_target as Function).apply(this, args);
|
|
};
|
|
}
|
|
|
|
@trackClass
|
|
class TestClass {
|
|
name = 'test';
|
|
|
|
@logMethod
|
|
modify() {
|
|
this.name = 'modified';
|
|
}
|
|
}
|
|
|
|
// Test that the class decorator worked
|
|
const instance = new TestClass();
|
|
console.log('Initial name:', instance.name);
|
|
console.log('Class was decorated:', decoratedClasses.includes(TestClass));
|
|
|
|
// Test that the method decorator worked
|
|
instance.modify();
|
|
console.log('Modified name:', instance.name);
|
|
|
|
console.log('Decorator test completed successfully!');
|