Typescript 1.8 - ES2015 imports in js files

1k Views Asked by At

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.

1

There are 1 best solutions below

4
On BEST ANSWER

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.