打字稿1.8 - 在js文件中导入ES2015

时间:2016-02-01 15:48:42

标签: javascript typescript ecmascript-6 babeljs typescript1.8

我想在Existing Babel项目上开始使用Typescript。我的目标是能够在构建过程中添加typescript,尽可能减少对现有代码的修改。出于这个原因,我决定连接打字稿(针对ES2015)和Babel。 有了ts1.8的js文件支持,我想我最终能够保持原样,然后逐个转换文件。 但这是我遇到的第一个问题:
error TS8003: 'export=' can only be used in a .ts file.

Typescript没有接缝允许es2015输出语法:
export default 'foo';
我们使用es2015语法进行导入/导出,我不想为旧的commonJS symtax​​更改它。有没有办法让打字稿允许它?

以下是演示此问题的最小示例:

hello.js

export default (name) => console.log(`Hello ${name}`);

tsconfig.json

{
    "version": "1.8",
    "compilerOptions": {
        "module": "es2015",
        "allowJs": true,
        "target": "es2015"
    }
}

命令行(使用typescript 1.8)

tsc --outDir ../out

结果

hello.js(1,1): error TS8003: 'export=' can only be used in a .ts file.

1 个答案:

答案 0 :(得分:3)

您为default export收到的错误是a bug in the TypeScript compiler。自您提交此问题以来,我已发出a fix

如果要在JavaScript文件中指定根模块(非标准且特定于某些模块加载器,如CommonJS),执行此操作的方法与在JavaScript中执行此操作的方式相同:

module.exports = yourRootExportObjectHere;

编译器应该识别并尊重它们等同于export =声明。

相关问题