【问题标题】:How to compile TypeScript's export default into exports = xxx如何编译 TypeScript export default 和 export = xxx
【发布时间】:2018-11-29 12:39:29
【问题描述】:

在 TypeScript 代码中,我像这样导出一个类:

export default class World {
}

但是我发现TypeScript编译成:

exports.default = class World {
}

那么如何编译成这样:

exports = class World {
}

【问题讨论】:

    标签: javascript typescript ecmascript-6 module


    【解决方案1】:

    使用语法export =:

    export = class World {
    }
    

    另请参阅:The handbook

    请注意,这与 ES6 模块不兼容。这种代码永远不能编译为原生 ES6 模块。

    为什么不能将export default 编译为exports =

    TypeScript 编译器以 CommonJS 和 AMD 格式生成成员 default,因为这是标准的工作方式。在 ES6 标准中:

    默认导出没有什么神奇之处;它就像任何其他导出一样,只是它被命名为"default"

    来源:ES6 In Depth: Modules,来自 Mozilla。

    【讨论】:

    • 那么可以将export default xxx编译成module.exports = xxx吗?
    【解决方案2】:

    在编译器设置试试,

    {
        module = "CommonJS",
        moduleResolution = "Node"
    }
    

    它应该编译成module.exports = ...

    【讨论】:

      猜你喜欢
      • 2017-01-09
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 2017-03-10
      • 1970-01-01
      相关资源
      最近更新 更多