导入打字稿类型引起的循环依赖 [英] Circular dependency caused by importing typescript type

查看:77
本文介绍了导入打字稿类型引起的循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用从服务器发送到我的Angular应用程序的打字稿中的数据进行建模. Opportunity具有forms属性,该属性包含Form[]对象的数组. Form具有parent属性,该属性可能包含Opportunity.为了解析类型,定义Opportunity的文件导入Form,定义Form的文件导入Opportunity.这将创建循环依赖项警告.

I'm modeling data in typescript sent from a server to my Angular app. An Opportunity has a forms property containing an array of Form[] objects. A Form has a parent property which may contain an Opportunity. In order to resolve the types, the file which defines Opportunity imports Form and the file which defines Form imports Opportunity. This creates a circular dependency warning.

我已经找到了一些关于循环依赖的先前SO问题(此处此处),但在每种情况下,它们都在处理javascript代码中的循环依赖关系.在这种情况下,循环依赖关系仅与打字稿类型有关,并且在编译后将不存在.有什么方法可以在文件中包括类型,同时避免这种循环依赖问题?到目前为止,我还没有发现任何东西.

I've found several prior SO questions dealing with circular dependencies (here, here), but in each case they were dealing with circular dependencies in the javascript code. In this case, the circular dependency is only with the typescript types, and will not exist after compilation. Is there some way to include a type in a file while avoiding this circular dependency issue? So far I haven't found anything.

我可以想到两种解决方案:

I can think of two solutions to this problem:

  1. 在同一文件中定义两个模型
  2. Opportunity文件中重新创建Form接口,在Form文件中重新创建Opportunity接口.
  1. Define both models in the same file
  2. Recreate the Form interface in the Opportunity file / the Opportunity interface in the Form file.

还有其他/更好的解决方案吗?谢谢!

Are there any other / better solutions? Thanks!

我似乎找到了 一个答案(由于某种原因,它确实在问题列表中远远落后).这个答案提出了两种可能性

I appear to have found an answer (it was just really far down in the list of questions for some reason). This answer suggestions two possibilities

  1. 创建一个单独的定义文件(这似乎涉及到重新创建OpportunityForm类接口,因此没有上面的选项#2更好).

  1. Create a seperate definition file (which would seem to involve recreating the Opportunity and Form class interfaces, so would be no better then option #2 above).

使用导入,这是我已经在做的(并且正在引起循环依赖警告).

Use import, which is what I'm already doing (and which is causing the circular dependency warning).

是否可以导入 just 类的关联接口?

Is there a way to import just the associated interface of a class?

请注意,当前OpportunityForm看起来像这样:

Just to be clear, currently Opportunity and Form look like this:

// opportunity.ts
import { Form } from '....../form'

export class Opportunity {
  public forms: Form[] = [];
}

// form.ts
import { Opportunity } from '....../opportunity'

export class Form {
  public parent: Opportunity;
}

推荐答案

您可以在form.ts文件中声明一个类,声明Opportunity {}类,TypeScript将假定该类是外部类,并且在运行时可用.您也可以跳过其中一个课程的导入.

You can declare a class declare class Opportunity {} in form.ts file, TypeScript will assume that class is an external class and will be available at runtime. And you can skip import in one of the class.

这里唯一的麻烦是,您必须声明要使用的方法,例如

The only pain here is, you have to declare methods that you will be using for example,

declare class Opportunity {
     method1(): void;
     method2(): number;
}

该类将用作简单的声明,并且不需要方法主体.并且VS intellisense将正常工作.

This class will serve as simple declaration and will not require method body. And VS intellisense will work correctly.

这篇关于导入打字稿类型引起的循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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