Typescript 1.8 - ES2015在js文件中导入 [英] Typescript 1.8 - ES2015 imports in js files

查看:145
本文介绍了Typescript 1.8 - ES2015在js文件中导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开始在现有的Babel项目中使用Typescript。我的目标是能够在构建过程中添加typescript,尽可能少的修改现有的代码。因此,我决定链接打字稿(瞄准ES2015)和巴别塔。
使用ts1.8的js文件支持,我以为我终于能够保持一切,然后一个一个地转换文件。
但这是我遇到的第一个问题:

错误TS8003:'export ='只能在.ts文件中使用。



Typescript没有接缝以允许es2015导出语法:

export default'foo';

我们使用es2015语法进行导出,我不想更改旧的commonJS symtax​​。有没有什么办法可以让打字稿允许?



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



hello.js

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

tsconfig.json



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ballowJs:true,
target:es2015
}
}

命令行(使用typescript 1.8)



tsc --outDir ../out



结果



hello.js(1,1):错误TS8003:'export ='只能在.ts文件中使用。

解决方案

您将获得默认导出的错误是 TypeScript编译器中的错误。自您提交这个问题后,我已发出修复



如果您要在JavaScript文件中指定根模块(对于某些模块装载程序(如CommonJS是非标准的和特定的),则执行此操作的方式与您所做的相同在JavaScript中:

  module.exports = yourRootExportObjectHere; 

编译器应该识别并尊重这些等价于 export = 声明。


I would like to start using Typescript on an Existing Babel project. My goal is to be able to add typescript to the build process with as less modifications as possible on the existing code. For this reason, I decided to chain typescript(targeting ES2015) and Babel. With ts1.8's js files support, I Thought I would finally be able to keep everything as is and then convert files one by one. But here is the first issue I encountered:
error TS8003: 'export=' can only be used in a .ts file.

Typescript doesn't seams to allow es2015 exports syntax:
export default 'foo';.
We are using es2015 syntax for imports/exports and I don't want to change it for the old commonJS symtax. Is there any way to make typescript allow it?

Here is a minimal example demonstrating the issue:

hello.js

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

tsconfig.json

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

command line (using typescript 1.8)

tsc --outDir ../out

result

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

解决方案

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.

这篇关于Typescript 1.8 - ES2015在js文件中导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆