I created a singleton (abstract) class that requires to have some data initialized before most methods can be used. This is an example of how the class looks like:
abstract class MyClass {
private static initialize(): void {
// do stuff
}
public static doStuff1(param1: string): string {
MyClass.initialize();
// do stuff
return 'stuff1';
}
public static doStuff2(param2: string[]): string[] {
MyClass.initialize();
// do stuff
return ['stuff2'];
}
}
Is there a way to avoid adding these initialize()
call on each methods? I was thinking of using decorators and found this related question: Typescript | Call a function every time a function is called
Unfortunately, it is using the legacy decorator format, at least this is the error I am getting:
Syntax error - Support for the experimental syntax 'decorators-legacy' isn't currently enabled
I was trying to find an equivalent format using the stage 2 proposal, but maybe there is a better option?