【问题标题】:Exporting an imported interface in TypeScript在 TypeScript 中导出导入的接口
【发布时间】:2017-05-31 19:03:18
【问题描述】:

我在不同的目录中有许多模板 - 我为每个模板定义了一个接口,因此我可以确定我在 TypeScript 代码中引用的内容将在模板中可用。

我想为每个接口定义一个接口,然后将所有接口整理到一个可以导入的文件中(这样我总是可以只导入该文件,自动完成将显示哪些接口可用)。

但是,我在执行此操作时遇到了麻烦。我目前拥有的:

login/interface:

export interface Index {
    error: string;
    value: string;
}

interfaces.ts:

import * as login from './login/interface';
export let View = {
    Login: login
};

login.ts:

import * as I from './interface';
...
let viewOutput: I.View.Login.Index = {
   ...
};

结果:

错误 TS2694:命名空间 '"...interface"' 没有导出的成员 'View'。

但是,如果我尝试将 I.View.Login.Index 作为一个简单变量访问,那么我会得到:

类型'typeof "...interface"'上不存在属性'Index'。

这似乎是正确的,因为接口并不是真正的类型,但它已经能够访问 I.View.Login 以达到这一目标。

恐怕我不明白我做错了什么或错过了什么。任何帮助将不胜感激!

【问题讨论】:

    标签: typescript


    【解决方案1】:

    你可以像这样re-export

    // interfaces.ts
    export * from './login/interface';
    export * from './something/interface';
    

    然后:

    // login.ts
    import * as I from './interface';
    let viewOutput: I.Index = { ... }
    

    另一种选择是:

    import * as login from './login/interface';
    export { login as Login };
    

    然后:

    // login.ts
    let viewOutput: I.Login.Index = { ... }
    

    【讨论】:

    • 谢谢你!我该怎么做才能将它包装在 View 对象中(例如 I.View.Login.Index。这将允许我基本上命名空间我的各种视图界面,​​但我似乎无法弄清楚它的语法。
    • 您可以使用export = 来做到这一点,尽管我认为这不是一个好主意
    猜你喜欢
    • 2015-08-23
    • 2017-04-17
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2018-07-02
    • 2016-12-26
    • 1970-01-01
    相关资源
    最近更新 更多