gpt4 book ai didi

javascript - 如何将类导出为根模块?

转载 作者:行者123 更新时间:2023-11-28 18:47:05 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com