I'm working on a project where I aim to create an easily extendible decorator factory, and achieving this would be a great accomplishment.
The main goal is to utilize the methods of a superclass as decorators for properties in subclasses.
This is typically how decorators are used:
import { handleSavePropertyNameDecorator } from "./W-ODecorator"
class Main {
@handleSavePropertyNameDecorator
test1: string = ""
}
export default Main
Here is the code snippet for the decorator function:
const propertyNames = [];
export const handleSavePropertyNameDecorator = (_instance: any, propertyName: string) => {
console.log("Executing handleSavePropertyNameDecorator")
propertyNames.push(propertyName);
}
Instead of creating a separate function for the decorator, my goal is to have the decorator function inherited from the superclass:
import SuperClass from "./SuperClass"
class Main extends SuperClass{
@this.handleDecoratorFunction
test1: string = ""
}
export default Main
class SuperClass {
static propertyNameArray: string[] = [];
protected handleDecoratorFunction(_instance: any, propertyName: string) {
console.log("executing handle decorator function from super class!")
SuperClass.propertyNameArray.push(propertyName);
}
}
export default SuperClass;
There is currently an issue with the keyword "this" triggering a compilation error due to possible undefined value. The decorator function is not being executed when I run the code.
Is there a workaround or alternative approach to make this idea feasible? Implementing this feature would greatly benefit the organization of my project.
Thank you for your assistance!