【问题标题】:How to correctly generate ES6 target with CommonJS modules in Typescript 1.7?如何在 Typescript 1.7 中使用 CommonJS 模块正确生成 ES6 目标?
【发布时间】:2015-12-17 15:05:15
【问题描述】:

在 Typescript 1.7 中添加了一个功能,允许分别针对目标语言系统和模块系统,例如它可以为最新节点生成带有 CommonJS 模块系统的 ES6 代码。但是,如果我尝试使用以下命令和源代码:

tsc *.ts --target es6 --module commonjs

 

// foo.ts
"use strict";
import Bar from './bar';
console.log(Bar);

 

// bar.ts
"use strict";
export default class Bar {}

令人惊讶的是,生成的代码生成了一些非常奇怪的导出符号:

// foo.js
"use strict";
var bar_1 = require('./bar');
console.log(bar_1.default);

 

// bar.js
"use strict";
class Bar {}
exports.Bar = Bar;

如您所见,bar.js 导致导出Bar 对象,而foo.js 尝试导入default 对象。当然,如果通过最新的nodejs v4.1.0 执行,这段代码会显示正在导入的“未定义”

任何提示为什么会出现这种奇怪的行为?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    您在 TS 1.7 中发现了一个错误。我相信这是正确的问题:

    我通过在 TS@next(版本 1.8.0-dev.20151216)中运行代码来验证它:

    npm install typescript@next --save
    node node_modules/typescript/bin/tsc --target es6 --module commonjs *.ts && node foo.js
    

    【讨论】:

    • 确实如此!非常感谢,我确信这个功能在官方的“新功能”列表中是他们最后一次测试它并且它有效的保障,所以我确信这是我的错,我对现代模块一无所知解决技术。
    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 2016-05-25
    • 2016-05-07
    • 1970-01-01
    • 2014-06-26
    • 2020-08-16
    • 2021-11-27
    • 2021-09-04
    相关资源
    最近更新 更多