如何在Typescript中为模块键入变量?

时间:2017-02-22 20:55:56

标签: typescript

给定文件

路径\ Example.ts:

export module Example {
    ... Stuff ...
}

和Test.ts:

import { Example }    from "Path/Example";

let exampleMock = getExampleMock(); // getExampleMock returns an any that matches the type structure of Example

let e = exampleMock as Example; // Errors: Cannot find name 'Example'
let local = Example; // local is typed as Example;
local = exampleMock ; // since exampleMock is an any, compiler allows this call.

... use local with full typing of Example module 

有没有办法向模块输入变量,而不先将其设置为真实模块?

1 个答案:

答案 0 :(得分:3)

尝试

let exampleMock = getExampleMock() as typeof Example;