【发布时间】:2022-12-02 04:21:13
【问题描述】:
I have many TS modules that contain nothing but TS Classes. Each of these classes contains a class decorator that adds the class information to a Map (in a different module, but that's not pertinent). The important part here is that the classes in my TS modules are never directly instantiated by name. Rather, the Map is accessed by other parts of the app which returns the class information, and that class information is used to instantiate the object.
My problem is because I am never directly accessing anything by name from the TS modules with the classes, the module isn't getting evaluated at runtime, and thus the class decorator isn't getting processed, so nothing gets added to my Map.
I was able to resolve this in two ways.
- In some other module I did a useless "new" on one of the classes in each module (one was enough to force the module to be evaluated).
- I put a dummy function in each TS module and called it from some other module during initialization.
Each of these work-arounds forced the module to be evaluated and thus the decorators were evaluated. However, this seems like a hack...
Is there any other way to force a module to be evaluated at runtime even though nothing is explicitly referenced by name elsewhere in the application? Imports alone do not force evaluation.
【问题讨论】:
标签: typescript class decorator evaluation