如何禁用 TypeScript Object.defineProperty(exports, "__esModule", { value: true })?

Li *_*yao 6 node.js typescript ecmascript-6

ts 编译器在每个文件中发出这一行:

Object.defineProperty(exports, "__esModule", { value: true });
Run Code Online (Sandbox Code Playgroud)

但是我的代码在 Nodejs 上运行,我没有编写 libaray,所以我认为这行代码对我来说是不必要的。我怎样才能禁用它?我的编译器选项是:

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "allowJs": true,
        "sourceMap": true,
        "outDir": "build",
        "moduleResolution": "Node",
        "lib": ["es6"]
    }
}
Run Code Online (Sandbox Code Playgroud)

例如,编译这个 ts 文件:

function add(a: number, b: number): number {
    return a + b
}

export { add }
Run Code Online (Sandbox Code Playgroud)

我有:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function add(a, b) {
    return a + b;
}
exports.add = add;
//# sourceMappingURL=App.js.map
Run Code Online (Sandbox Code Playgroud)

如何删除第二行?

bas*_*rat -17

但我的代码运行在 Nodejs 上,我没有编写 libaray,所以我认为这一行对我来说是不必要的。我怎样才能禁用它

效果很好。所以不要删除它。想像一下"use strict"。这不是必需的,但有它就好。

它的存在是为了允许转译器之间的互操作(typescript / babel /其他未来的转译器)。

  • 这不是答案,这是意见。 (7认同)