没有导入类的TypeScript类型?

时间:2017-05-09 21:50:04

标签: class typescript types dependency-injection inversion-of-control

我即将尝试在我们的应用程序中实现一些IoC / DI框架(考虑Inversify,如果有任何其他建议)但我们仍然有必须为其类型包含文件的问题。我们如何解决这个问题?我们正在使用最新版本的TypeScript(编写本文时为2.3)。

我们为各种类型提供了大量导入,例如:

// Components
import {GuideTime} from '../components/guide-time/guide-time.component';
import {GuideNetwork} from '../components/guide-network/guide-network.component';
import {GuideDetail} from '../components/guide-detail/guide-detail.component';
import {Player} from '../components/player/player.component';

// Services
import {Keys, KeyService} from '../core/services/key.service';
import {GuideService} from '../core/services/guide/guide.service';
import {afterCSSUpdate} from '../core/services/utility.service';
import {DataService} from '../core/services/data/data.service';

或者只是为了得到某种东西:

import {ITitle} from '../../models/title.interface';
import {IGuide} from '../../models/guide.interface';

有没有办法整合这些(最好是自动或全局)?我意识到我可以在某个地方创建一个mytypes.ts文件,然后只需从我们的应用程序中导入和导出所有类型,这样就可以导入单个中央文件,但这会引入其他问题(以及大量工作)。

谢谢!

1 个答案:

答案 0 :(得分:1)

要使所有这些全局化,您需要将它们全部导入到单个接口,然后将该接口导入到需要访问每种类型的任何位置。类似于

的东西
import * as mainInterface from 'MainInterface'

此外,您可以尝试仅导入副作用(这样可以访问这些类型),但实际上不建议这样做。

来自the documentation

  

虽然不推荐练习,但有些模块设置了一些可供其他模块使用的全局状态。这些模块可能没有任何出口,或者消费者对其任何出口都不感兴趣。要导入这些模块,请使用:import "./my-module.js";

EDIT 此外,与上述单一界面解决方案类似,您可以将每种类型放入同一名称空间。您可以spread a namespace across multiple files using this approach,这意味着您可以将每种类型包装在主命名空间中。

namespace MainNamespace {

    export class GuideTime {
        ...
        }
    ...
}