合并来自多个文件的模块

时间:2015-12-11 22:15:36

标签: typescript

是否可以合并模块拆分多个文件的声明?即我想做这样的事情:

文件Foo.ts



import {Foo} from "./Foo1";
import {Foo} from "./Foo2";

export module Foo {
    export const foo = "Foo!";
}

in file Foo1.ts

export module Foo {
   export const foo1 = "Foo 1!";
}

in file Foo2.ts

export module Foo {
   export const foo2 = "Foo 2!";
}

in file Bar.ts

import {Foo} from "./Foo";

//access Foo.foo, Foo.foo1, Foo.foo2

However, the compiler complains about

中的

1 个答案:

答案 0 :(得分:1)

创建文件Foo.ts

export * from 'foo1';
export * from 'foo2';

也避免export module ...。只需按原样导出所需的变量/函数。

Foo1:

export const foo1 = "Foo 1!";

foo2的:

export const foo2 = "Foo 2!";

现在在Bar.ts你可以

import {foo1, foo2} from 'foo';

甚至:

import * as Foo from 'foo';