Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transpilation of ES6 imports is inconsistent with babel #5458

Closed
eggers opened this issue Oct 29, 2015 · 7 comments
Closed

Transpilation of ES6 imports is inconsistent with babel #5458

eggers opened this issue Oct 29, 2015 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@eggers
Copy link

eggers commented Oct 29, 2015

Currently, babel and typescript handle transpilation of ES6 imports differently.

The following works in typescript:

import * as rp from 'request-promise';

rp('http://github.com').then(console.log);

but I get an error when running it with babel's transpilation:

TypeError: object is not a function
    at Object.<anonymous> (./test.js:7:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

To get it to work with babel, I have to do the following:

import rp from 'request-promise';

but that returns an error when compiling with typescript:

test.ts(1,8): error TS1192: Module ''request-promise'' has no default export.

I currently have a pipeline of typescript --tsc--> es6 --babel--> es5 so that I can use async/await while I wait for TypeScript to support it for ES5. However, that means I have to modify all my DefinitelyTyped declarations to have a default export so that tsc doesn't complain.

Discussions of which standard is better for transpilation put aside, my feeling is that TypeScript should follow the standard set by babel rather than using their own since babel is further along at supporting es6, and has a larger community at this point:

npm babel

33,936 downloads in the last day
174,998 downloads in the last week
551,455 downloads in the last month

npm typescript

15,303 downloads in the last day
101,148 downloads in the last week
357,086 downloads in the last month

In addition to keeping in step with the larger community, it would help adoption of typescript by making the transition to typescript from babel more seamless.

@jkillian
Copy link

@eggers I ran into this same problem today, using the same sort of build pipeline. I ended up with the same solution as you - changing typing files to have an export default.

In your first case though, you can also do something like this if you don't want to modify your typings. I'm not sure if you'll be able to have typing support for rp though this way:

import * as _rp from 'request-promise';
const rp = (_rp as any).default;
rp('http://github.com').then(console.log);

@eggers
Copy link
Author

eggers commented Oct 30, 2015

@jkillian

Yeah, man that is so ugly. I also don't like the solution because when typescript does finally support async/await --> ES5, and I drop babel from my pipeline, it'd break my code.

Also, there are even some other issues with typing support for request-promise because the function can take an Options parameter that has all optional properties, meaning the following is valid typescript:

rp(666).then(console.log);

I put more details here: DefinitelyTyped/DefinitelyTyped#6489

@mhegazy
Copy link
Contributor

mhegazy commented Oct 30, 2015

this looks like a typing issue. if request-promise has a default export it should be in the .d.ts file.

@jkillian
Copy link

There's already an export = in the typings, but that has to be changed to export default to work correctly with TS and Babel at the moment

@eggers
Copy link
Author

eggers commented Oct 30, 2015

@mhegazy The declaration is accurate for how typescript handles the transpilation, but the issue is that babel handles transpilation differently, and when I go ts-->es6-->es5 for babel's async/await support, i have to make declaration changes.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 13, 2015

Looks like this will be handled by #5577

@mhegazy
Copy link
Contributor

mhegazy commented Nov 13, 2015

see #5285 for more details.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 13, 2015
@mhegazy mhegazy closed this as completed Nov 13, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants