SystemJS中的TypeScript继承和循环依赖关系 [英] TypeScript inheritance and circular dependencies in SystemJS

查看:778
本文介绍了SystemJS中的TypeScript继承和循环依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个非常大的项目中使用带有 - 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:


  1. 它会暂停 class A's 分辨率,并尝试加载 class B 依赖关系

  2. class B 将认为它的依赖关系被解决,因为 A类已被触动。

  3. B类继承将无法解决,因为 A类仍然未定义。

  1. It will pause class A's resolution and will try to load the class B dependency
  2. class B will think its dependencies are resolved, since class A has been touched.
  3. class B's inheritance will fail to resolve, because class A is still undefined.

我可以在网络上找到与模块加载程序的循环依赖关系的大多数解决方案是:

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继承和循环依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆