【问题标题】:How to force contents of a typescript module to be evaluated at runtime?How to force contents of a typescript module to be evaluated at runtime?
【发布时间】: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.

  1. 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).
  2. 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


    【解决方案1】:

    My solution was to import the each TS module without specifying any particular function or type from the module. This caused the module to be evaluated.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-01
      • 2022-12-27
      • 2022-12-02
      • 2022-12-01
      • 2022-12-01
      • 2022-12-19
      • 2022-12-01
      • 2022-10-19
      相关资源
      最近更新 更多