简体   繁体   English

Typescript 1.8 - 在js文件中导入ES2015

[英]Typescript 1.8 - ES2015 imports in js files

I would like to start using Typescript on an Existing Babel project. 我想在现有的Babel项目上开始使用Typescript。 My goal is to be able to add typescript to the build process with as less modifications as possible on the existing code. 我的目标是能够在构建过程中添加typescript,尽可能减少对现有代码的修改。 For this reason, I decided to chain typescript(targeting ES2015) and Babel. 出于这个原因,我决定连接打字稿(针对ES2015)和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. 有了ts1.8的js文件支持,我想我最终能够保持原样,然后逐个转换文件。 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: Typescript没有接缝允许es2015导出语法:
export default 'foo'; .
We are using es2015 syntax for imports/exports and I don't want to change it for the old commonJS symtax. 我们使用es2015语法进行导入/导出,我不想为旧的commonJS symtax​​更改它。 Is there any way to make typescript allow it? 有没有办法让打字稿允许它?

Here is a minimal example demonstrating the issue: 以下是演示此问题的最小示例:

hello.js hello.js

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

tsconfig.json tsconfig.json

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

command line (using typescript 1.8) 命令行(使用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 . 您为default export 获得的错误是TypeScript编译器中的错误 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: 如果要在JavaScript文件中指定根模块(这是非标准的,并且特定于某些模块加载器,如CommonJS),那么执行此操作的方法与在JavaScript中执行此操作的方式相同:

module.exports = yourRootExportObjectHere;

The compiler should recognize and respect these as equivalent to export = declarations. 编译器应该识别并尊重它们等同于export =声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM