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

最佳答案

出口= 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/

10-09 17:41