使用export = statement从模块导入接口

时间:2018-01-21 15:33:52

标签: typescript

许多类型定义文件使用export =指令,例如:

declare module "i40" {
    interface RouterStatic {
        () : Router;
    }

    interface RouteInfo {
        params : {
            [key : string] : any;
        };
        splats : string[];
        route : string;
        fn : Function;
        next : any;
    }

    interface Router {
        addRoute(routeString : string, action ?: Function);
        match(test : string) :RouteInfo;
    }

    export = null as RouterStatic;
}

或者,有人可能会编写如下代码:

export interface Blah {}
const x = {hi : 5};
export = x;

编辑:此代码曾经工作过一次,但从当前版本(2.6)开始,它将无法编译。编译器说如果我从模块中导出其他东西,我就不能使用export =。这是有道理的。

如何导入模块中的其中一个接口?以下都不起作用。

import Router = require('i40');
let x : Router.RouteInfo; //RouteInfo not found

import {RouteInfo} from 'i40'; //RouteInfo not found

import * as Router2 from 'i40'; //Error, i40 is not a module

1 个答案:

答案 0 :(得分:2)

我说(Ambient Modules下的参考modules

declare module "i40" {
  interface RouterStatic {
    ...
  }

  interface RouteInfo {
    ...
  }

  interface Router {
    ...
  }

  export { RouterStatic, RouteInfo, Router } as RouterStatic;
}

import * as Router2 from 'i40';
// use as Router2.RouterStatic,  etc

// or  
import { RouterStatic, RouteInfo, Router } from 'i40';
// use as RouterStatic,  etc