当没有模块时,如何在命名空间中包含函数?
当没有模块时,如何在命名空间 /模块中包含功能?
示例:我有一个命名空间:
namespace Java {}
在打字稿中,通常在我们需要命名空间或模块中的函数时,我们添加“导出”:
namespace Java {
export function version() {
return "java8";
}
}
namespace JavaScript {
function JavaVer() {
return Java.version();
}
}
但是如果我将模块设置为无:
// tsconfig
"module": "none",
无需
website/sourcecode/main.ts:17:21 - error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.
如果模块 版本(){}
那我该怎么办?
How to include functions in namespace / module when don't have module?
example: I have a namespace:
namespace Java {}
in typescript, usually when we need a function in namespace or module, we add 'export':
namespace Java {
export function version() {
return "java8";
}
}
namespace JavaScript {
function JavaVer() {
return Java.version();
}
}
But if I set module to none:
// tsconfig
"module": "none",
If module is none, I cannot use import / export:
website/sourcecode/main.ts:17:21 - error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.
17 export function version() {}
so what can I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用普通对象:
ts游乐场
You can use a plain object:
TS Playground
它在这里得到了回答:实际上是“最高级别的任何进口或出口”。命名空间内部的导入和导出很好,因为它们在运行时不会从.TS文件中导出任何内容,并且只要文件保留“不是模块”模块=无工作正常。长解释在这里
It was answered in here: It's actually "can't have any imports or exports at the top level". Imports and exports inside namespaces are fine because they don't export anything from .ts file at runtime, and as long as file remains 'not a module' module=None works fine. Long explanation here