在 TypeScript 中导出导入的接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41684900/
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
Exporting an imported interface in TypeScript
提问by Allan Jardine
I have a number of templates in different directories - I define an interface for each so I can be sure that what I am referencing in my TypeScript code will be available in the template.
我在不同的目录中有许多模板 - 我为每个模板定义了一个接口,这样我就可以确定我在 TypeScript 代码中引用的内容将在模板中可用。
I would like to define an interface for each of them and then collate all of the interfaces in a single file that can be imported (so I can always just import that file and auto complete will show what interfaces are available).
我想为每个接口定义一个接口,然后将所有接口整理到一个可以导入的单个文件中(所以我总是可以只导入该文件,自动完成将显示哪些接口可用)。
However, I'm having trouble doing this. What I currently have:
但是,我在执行此操作时遇到了麻烦。我目前拥有的:
login/interface
:
login/interface
:
export interface Index {
error: string;
value: string;
}
interfaces.ts
:
interfaces.ts
:
import * as login from './login/interface';
export let View = {
Login: login
};
login.ts
:
login.ts
:
import * as I from './interface';
...
let viewOutput: I.View.Login.Index = {
...
};
Results in:
结果是:
error TS2694: Namespace '"...interface"' has no exported member 'View'.
错误 TS2694:命名空间 '"...interface"' 没有导出成员 'View'。
However, if I try to access I.View.Login.Index
as a simple variable then I get:
但是,如果我尝试I.View.Login.Index
作为一个简单的变量访问,那么我会得到:
Property 'Index' does not exist on type 'typeof "...interface"'.
属性 'Index' 不存在于类型 'typeof "...interface"'。
Which seems correct since the interface is not really a type, but it has been able to access I.View.Login
to get that far.
这似乎是正确的,因为接口并不是真正的类型,但它已经能够访问I.View.Login
到那么远。
I don't understand what I'm doing wrong or missing here I'm afraid. Any help would be greatly appreciated!
恐怕我不明白我做错了什么或错过了什么。任何帮助将不胜感激!
回答by Nitzan Tomer
You can re-exportlike so:
您可以像这样重新导出:
// interfaces.ts
export * from './login/interface';
export * from './something/interface';
And then:
接着:
// login.ts
import * as I from './interface';
let viewOutput: I.Index = { ... }
Another option is:
另一种选择是:
import * as login from './login/interface';
export { login as Login };
And then:
接着:
// login.ts
let viewOutput: I.Login.Index = { ... }