TypeScript 中的循环类型引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24444436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Circular Type References in TypeScript
提问by Viper Bailey
I am new to typescript and am trying to understand how I can setup a circular reference between two types. The reference need not be a full code reference, simply the interfaces, but with interfaces defined in separate files. For example, let's say I have two interfaces: Parent and Child. They are doubly-linked such that the parent has a collection of children and each child has a reference to the parent (as seen below). How do I setup the imports or dependencies so that these can be defined in separate files?
我是打字稿的新手,并试图了解如何在两种类型之间设置循环引用。引用不需要是完整的代码引用,只是接口,而是在单独的文件中定义的接口。例如,假设我有两个接口:Parent 和 Child。它们是双重链接的,这样父级有一个子级集合,每个子级都有一个对父级的引用(如下所示)。如何设置导入或依赖项,以便可以在单独的文件中定义它们?
interface Parent {
children: Child[]
}
interface Child {
parent: Parent
}
采纳答案by Bruno Grieder
Two solutions below. I prefer the latter since it offers clean interfacing with Node JS modules, but unfortunately my IDE doesn't (yet) like it as much as I do...
下面两个解决方案。我更喜欢后者,因为它提供了与 Node JS 模块的干净接口,但不幸的是,我的 IDE(还)不像我那样喜欢它......
Use references
使用参考
Create a definitions.d.ts
file that will only contain the references to your classes/interfaces
创建一个definitions.d.ts
仅包含对您的类/接口的引用的文件
/// <reference path="Parent.ts" />
/// <reference path="Child.ts" />
In Parent.ts
and Child.ts
, point to a single reference, the definitions.d.ts
file
在Parent.ts
and 中Child.ts
,指向单个引用,definitions.d.ts
文件
/// <reference path="definitions.d.ts" />
Use import...require
使用导入...需要
pass the --module commonjs
flag to tsc
then import
what you require
and export
what you want to expose
在传递--module commonjs
标志tsc
,然后import
你require
和export
你想暴露什么
In Parent.ts
在 Parent.ts
import Child = require('Child')
interface Parent {
children: Child[]
}
export = Parent
In Child.ts
在 Child.ts
import Parent = require('Parent')
interface Child {
parent: Parent
}
export = Child
Please note, that you do not specify the extension '.ts' in require
请注意,您没有在中指定扩展名“.ts” require
EDIT Sept 2016
编辑 2016 年 9 月
It is now better to use ES6 style imports (and avoid default exports):
现在最好使用 ES6 样式导入(并避免默认导出):
Parent.ts
父母.ts
import {?Child } from './Child'
export interface Parent {
children: Child[]
}
Child.ts
Child.ts
import {?Parent } from './Parent'
export interface Child {
parent: Parent
}
回答by Lancer.Yan
I have about 10 ts files , in a Circular-Dependency-Hell .
我有大约 10 个 ts 文件,位于 Circular-Dependency-Hell 中。
The common methods can't help me any more , because the dependencies relation between 10 files is to complex.
常用的方法已经帮不上忙了,因为10个文件之间的依赖关系太复杂了。
At finally , I solved it. Using following 2 methods :
最后,我解决了它。使用以下2种方法:
Install repo ———— "circular-dependency-plugin": "5.0.2"
This repo will helps me to find the place where circular occurs.
Using a designed internal.ts , to manage my import & export
I tried the method of this article :
How to fix nasty circular dependency issues once and for all in JavaScript & TypeScript
This amazing article tells me to create internal.ts .
and using like
export * form 'file-A' ; export * from 'file-B'
to manage my circular dependencies.It works very well when I use dependencies related to my 10 files, like this
import classA from '../internal.ts'
.
安装 repo ———— "circular-dependency-plugin": "5.0.2"
这个 repo 将帮助我找到循环发生的地方。
使用设计的 internal.ts 来管理我的导入和导出
我尝试了这篇文章的方法:
如何在 JavaScript 和 TypeScript 中一劳永逸地解决讨厌的循环依赖问题
这篇很棒的文章告诉我创建 internal.ts 。
并使用 like
export * form 'file-A' ; export * from 'file-B'
来管理我的循环依赖项。当我使用与我的 10 个文件相关的依赖项时,它工作得很好,就像这样
import classA from '../internal.ts'
.
————————————————————————————————————
———————————————————————————————————————
If the above method has no effect on you, I found another general solution:
如果上面的方法对你没有影响,我找到了另一个通用的解决方案:
Use
利用
const File_Promise = import ('yourFilePath')"
to import other file or module .
const File_Promise = import ('yourFilePath')"
导入其他文件或模块。
when you need to use this one, use
当你需要使用这个时,使用
File_Promise.then (file => { file.xxx(file.yyy) })
, just like using Promise syntax.
`
File_Promise.then (file => { file.xxx(file.yyy) })
,就像使用 Promise 语法一样。`
This will break the Circular-Dep Chain !
这将打破循环深度链!
If i am you , I will continue this action until NO ERROR Reported by "circular-dependency-plugin".
如果我是你,我会继续这个动作,直到“循环依赖插件”没有报告错误。
————————————————————————————————————
———————————————————————————————————————
Hope to help YOU !
希望能帮到你!