Skip to content

Transpilation of ES6 imports is inconsistent with babel #5458

Closed

Description

@eggers

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.

Activity

  1. jkillian commented on Oct 30, 2015

    @jkillian

    Jacob Eggers (@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);
  2. eggers commented on Oct 30, 2015

    @eggers
    Author

    Jason Killian (@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

  3. mhegazy commented on Oct 30, 2015

    @mhegazy
    Contributor

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

  4. jkillian commented on Oct 30, 2015

    @jkillian

    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

  5. eggers commented on Oct 30, 2015

    @eggers
    Author

    Mohamed Hegazy (@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.

  6. mhegazy commented on Nov 13, 2015

    @mhegazy
    Contributor

    Looks like this will be handled by #5577

  7. mhegazy commented on Nov 13, 2015

    @mhegazy
    Contributor

    see #5285 for more details.

  8. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions