提问人:Lucio Flores 提问时间:12/17/2022 更新时间:12/17/2022 访问量:318
从 Typescript 中的多个文件导出同一命名空间中的符号
Export symbols in same namespace from a multiple files in Typescript
问:
我有一个打字稿模块,想从多个文件的命名空间“aaa”中定义符号。
a.ts:
export namespace aaa {
export const a = "a";
}
b.ts:
export namespace aaa {
export const b = "b";
}
index.ts:
export * from "./a";
export * from "./b";
在index.ts的第二行中,我收到以下警告:
TS2308: Module "./b" has already exported a member named 'aaa'. Consider
explicitly re-exporting to resolve the ambiguity.
如何在多个文件中定义同一命名空间中的符号,并将它们全部导出index.ts?
答:
0赞
Cody Duong
12/17/2022
#1
您可以使用扩展语法合并模块及其唯一命名空间。aaa
index.ts
import * as A from "./a";
import * as B from "./b";
export default { ...A.aaa, ...B.aaa };
// or if you prefer a named export
export const aaa = { ...A.aaa, ...B.aaa };
someotherfile.ts
import aaa from "aaa"
const A = aaa.A
但是请注意,扩展语法是浅合并,因此 和 之间的任何冲突声明都将选择采用 的声明。a.ts
b.ts
b.ts
a.ts
export namespace aaa {
export const foo = "foo";
}
b.ts
export namespace aaa {
export const foo = "bar";
}
->
someotherfile.ts
import aaa from "aaa"
const A = aaa.foo
console.log(A) // -> 'bar'
评论