我正在使用 TypeScript 构建 Node 项目。当 TS 将我的类编译为 JS 时,似乎导出了不需要的额外深度,请允许我说明一下。
TS
export class AppInfo {
public name: string;
public version: string;
constructor(name: string, version: string) {
this.name = name;
this.version = version;
}
}
生成的JS
var AppInfo = (function () {
function AppInfo(name, version) {
this.name = name;
this.version = version;
}
return AppInfo;
})();
exports.AppInfo = AppInfo;
所以如果我想导入这个类,我必须这样做:
const AppInfo = require("./AppInfo").AppInfo;
var appInfo = new AppInfo(...);
如何告诉 TypeScript 直接导出我的类?像这样:
exports = AppInfo;
最佳答案
exports = AppInfo;
使用export =
,如下所示:
class AppInfo {
public name: string;
public version: string;
constructor(name: string, version: string) {
this.name = name;
this.version = version;
}
}
export = AppInfo;
关于javascript - 如何将类导出为根模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35121795/