SystemJS中的TypeScript继承和循环依赖关系

编程入门 行业动态 更新时间:2024-10-21 09:51:30
本文介绍了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:

  • 它会暂停 class A's 分辨率,并尝试加载 class B 依赖关系
  • class B 将认为它的依赖关系被解决,因为 A类已被触动。
  • B类继承将无法解决,因为 A类仍然未定义。
  • It will pause class A's resolution and will try to load the class B dependency
  • class B will think its dependencies are resolved, since class A has been touched.
  • 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继承和循环依赖关系

    本文发布于:2023-11-17 02:15:11,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1608437.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:关系   SystemJS   TypeScript

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!