我在一个非常大的项目中使用带有 - module system (SystemJS)的TypeScript。 SystemJS支持循环依赖,大多数时候它工作正常。但是,当涉及到TypeScript继承时,事情就会中断。
I'm using TypeScript with --module system (SystemJS) in a very large project. SystemJS supports cyclic dependencies, and most of the time it works fine. However, when TypeScript inheritance gets involved, things begin to break.
例如,如果 A类取决于 class B 和 class B 从 A类继承,那么如果 A类首先被加载:
For example, if a class A depends on class B, and class B inherits from class A, then if class A gets loaded first:
我可以在网络上找到与模块加载程序的循环依赖关系的大多数解决方案是:
Most of the "solutions" I can find on the web to circular dependencies with module loaders are either:
- 更改您的设计/将课程合并为单个模块
- CommonJS和非TypeScript特定解决方法
我觉得循环设计有合理的理由,并结合类int o巨型文件并不总是可取的,所以请考虑这些解决方法是关于我问的问题的主题。
I feel like there are valid justifications for circular designs, and combining classes into giant files is not always desirable, so please consider these workarounds to be off topic for the question that I am asking.
有没有解决实际问题的方法?
Are there any solutions to the actual problem?
推荐答案更改您的设计是最有利的解决方案。一个类不应该依赖它的子类。如果您在工厂中使用它们,那是一个单独的问题,应该在一个单独的类/功能/模块中。
Changing your design is the most favourable solution. A class should not depend on its subclasses. If you use them in a factory or so, that is a separate concern and should go in a separate class/function/module.
是有任何实际问题的解决方案?
Are there any solutions to the actual problem?
正如你所说,问题只在模块A首先加载时才会出现。解决方案是防止这种情况,并写入一个额外的模块,作为A及其所有子类的代理,同时以正确的顺序导入它们。
As you said, the problem occurs only when module A is loaded first. The solution is to prevent that, and write an extra module that acts as a proxy to A and all its subclasses while importing them in the correct order.
更多推荐
SystemJS中的TypeScript继承和循环依赖关系
发布评论