我使用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;
答案 0 :(得分:3)
exports = AppInfo;
使用export =
,如下所示:
class AppInfo {
public name: string;
public version: string;
constructor(name: string, version: string) {
this.name = name;
this.version = version;
}
}
export = AppInfo;
答案 1 :(得分:0)
而不是
exports.AppInfo = AppInfo;
使用
module.exports = AppInfo;