Typescript: Using export-as emits __importStar and related helpers when importHelpers: true

Created on 28 Feb 2020  ·  3Comments  ·  Source: microsoft/TypeScript


TypeScript Version: Version 3.9.0-dev.20200228


Search Terms:

export as importhelpers
export as importstar
export as helper

Code

export * as _ from 'lodash-es';

compiled with the following configuration:

{
  "compilerOptions": {
    "target": "es2020",
    "module": "commonjs",
    "importHelpers": true,
    "esModuleInterop": true
  }
}

Expected behavior:

The output should be the same as if compiling the equivalent older TS:

import * as _ from 'lodash-es';
export {_}

i.e. the compiled Javascript should be:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
// export * as _ from 'lodash-es';
const _ = tslib_1.__importStar(require("lodash-es"));
exports._ = _;

Actual behavior:

The helper functions are emitted:

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports._ = __importStar(require("lodash-es"));

Playground Link:

The Typescript Playground doesn't support importHelpers, and the analogue noEmitHelpers doesn't cause this behaviour; I don't believe it's possible to reproduce this issue using the Playground.

Related Issues:

The similar issues search pointed me at #21560 and #27415 which also mention import helpers being inlined in different edge cases. Seeing as export-as isn't a dynamic import, and the older issue related to regular imports is closed, this seems to be a different, possibly related issue.

Bug Rescheduled

Most helpful comment

The "__setModuleDefault" helper is causing this error for me:

TypeError: Cannot redefine property: default

I did not encounter this under 3.8.

All 3 comments

The "__setModuleDefault" helper is causing this error for me:

TypeError: Cannot redefine property: default

I did not encounter this under 3.8.

@lotz same for me with the module cross-fetch
Started with 3.9.2

I fixed it by named importing fetch as it is also named exported not only default exported.

We should be treating export * as ns from ... the same as import * as ns from ... in collectExternalModuleInfo. I should have a fix up shortly.

@lotz: I'm not certain whether your issue is related, and may have been fixed by #38808. Can you verify whether that is the case, and if not, can you provide a specific repro for your case?

Was this page helpful?
0 / 5 - 0 ratings