3

我想在现有的 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 符号更改它。有没有办法让打字稿允许它?

这是一个演示该问题的最小示例:

你好.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.

4

1 回答 1

3

The error you're getting for the default export is a bug in the TypeScript compiler. I've sent out a fix since you filed this issue.

If you want to specify the root module in JavaScript files (which is non-standard and specific to certain module loaders like CommonJS), the way to do this is the same way you'd do this in JavaScript:

module.exports = yourRootExportObjectHere;

The compiler should recognize and respect these as equivalent to export = declarations.

于 2016-02-01T21:10:31.917 回答