Skip to content

__importDefault without tslib in output when noEmitHelpers and importHelpers set to true #40328

Closed

Description

TypeScript Version: 4.0.2

Search Terms:

tslib, __importDefault

Code

Using "noEmitHelpers": true and "importHelpers": true does not always include tslib_1 in files, in the last example __importDefault is in the index.js file, without any tslib_1

tsconfig

{
  "compileOnSave": false,
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "es2017", "dom", "es2018.promise"
    ],
    "module": "commonjs",
    "moduleResolution": "node",
    "rootDir": "./src",
    "declaration": true,
    "noLib": false,
    "outDir": "target/lib/ts",
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "strictBindCallApply": false,
    "esModuleInterop": true,
    "strictNullChecks": true,
    "noUnusedLocals": true,
    "noImplicitAny": true,
    "noEmitHelpers": true,
    "importHelpers": true
  },
  "exclude": [
    "node_modules",
    "target"
  ]
}

TypeScript 3.9.7

// index.ts
export * from "./core/Core";
export {default as HandlerManager} from "./core/HandlerManager";

// becomes

// index.js
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
tslib_1.__exportStar(require("./core/Core"), exports);
var HandlerManager_1 = require("./core/HandlerManager");
Object.defineProperty(exports, "HandlerManager", { enumerable: true, get: function () { return HandlerManager_1.default; } });

Here tslib_1 is only used for exportStar and HandlerManager is not wrapped in __importDefault

TypeScript 4.0.2

// index.ts
export * from "./core/Core";
export {default as HandlerManager} from "./core/HandlerManager";

// becomes

// index.js
Object.defineProperty(exports, "__esModule", { value: true });
// lots of exports, removed because only HandlerManager is relevant
exports.firstExport = ... = exports.HandlerManager = void 0;
var tslib_1 = require("tslib");
tslib_1.__exportStar(require("./core/Core"), exports);
var HandlerManager_1 = require("./core/HandlerManager");
Object.defineProperty(exports, "HandlerManager", { enumerable: true, get: function () { return tslib_1.__importDefault(HandlerManager_1).default; } });

Here HandlerManager is imported correct with tslib_1, however if we remove the * import from index.ts, we have

// index.ts
// export * from "./core/Core";
export {default as HandlerManager} from "./core/HandlerManager";

// becomes

// index.js
Object.defineProperty(exports, "__esModule", { value: true });
// lots of exports, removed because only HandlerManager is relevant
exports.firstExport = ... = exports.HandlerManager = void 0;
// export * from "./core/Core";
var HandlerManager_1 = require("./core/HandlerManager");
Object.defineProperty(exports, "HandlerManager", { enumerable: true, get: function () { return __importDefault(HandlerManager_1).default; } });

Now we have __importDefault , but tslib is not imported and __importDefault doesn't come from tslib, so __importDefault is undefined

Expected behavior:

tslib should be imported in index.js and importDefault should be called like tslib_1.__importDefault

Actual behavior:

__importDefault without any tslib in index.js

Playground Link: Not possible, requires more than 1 file

Related Issues: No similar issues

Activity

  1. mxmauro commented on Sep 5, 2020

    @mxmauro

    Here the same, cannot get rid of __importDefaut

  2. added
    Needs Investigation
    on Sep 9, 2020
  3. mrttrifork commented on Nov 23, 2020

    @mrttrifork
    Author

    I see that 4.1.2 is released. Please consider to include this issue in 4.2

  4. added
    Rescheduled
    on Dec 11, 2020
  5. mrttrifork commented on Mar 16, 2021

    @mrttrifork
    Author

    The problem still persists. As for now i have created a workaround in my index.ts

    I added this at the top of the file

    export * from "./noop"; // TypeScript 4.2 is bugged it won't import tslib.__importDefault without importing another tslib function

    Where noop.ts looks like this

    export function noop() { /* Do nothing */ }

    This way i get __exportStar imported and then everything works as intended

  6. 6 remaining items

  7. VitorLuizC commented on Feb 14, 2022

    @VitorLuizC

    Still happening in TypeScript 4.5.5.

  8. piyushkmr commented on Apr 28, 2022

    @piyushkmr

    Still happening with 4.6.3
    How to get rid of __importDefault, earlier implementation was clean, and it was less code.
    Now if I add "noEmitHelpers": true,, it adds 3 lines of code to every file. If I don't add it, it doesn't works.

  9. thynson commented on Jun 6, 2023

    @thynson

    Still happens in Typescript 5.1.0

  10. thynson commented on Jun 7, 2023

    @thynson

    As far as I can tell, this issue seems unlikely to happen when "esModuleInterop" is disabled, while "allowSyntheticDefaultImports" is irrelevant to this issue and can still be enabled.

  11. Shamilik commented on Sep 25, 2023

    @Shamilik

    Got into same problem on 4.9.5 with

    export { default as stringify } from 'fast-json-stable-stringify';
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.stringify = void 0;
    var fast_json_stable_stringify_1 = require("fast-json-stable-stringify");
    Object.defineProperty(exports, "stringify", { enumerable: true, get: function () { return __importDefault(fast_json_stable_stringify_1).default; } });

    fixed with

    import { default as stringify } from 'fast-json-stable-stringify';
    export { stringify };
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.stringify = void 0;
    var tslib_1 = require("tslib");
    var fast_json_stable_stringify_1 = tslib_1.__importDefault(require("fast-json-stable-stringify"));
    Object.defineProperty(exports, "stringify", { enumerable: true, get: function () { return fast_json_stable_stringify_1.default; } });
  12. thynson commented on Sep 26, 2023

    @thynson

    Got into same problem on 4.9.5 with

    export { default as stringify } from 'fast-json-stable-stringify';
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.stringify = void 0;
    var fast_json_stable_stringify_1 = require("fast-json-stable-stringify");
    Object.defineProperty(exports, "stringify", { enumerable: true, get: function () { return __importDefault(fast_json_stable_stringify_1).default; } });

    fixed with

    import { default as stringify } from 'fast-json-stable-stringify';
    export { stringify };
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.stringify = void 0;
    var tslib_1 = require("tslib");
    var fast_json_stable_stringify_1 = tslib_1.__importDefault(require("fast-json-stable-stringify"));
    Object.defineProperty(exports, "stringify", { enumerable: true, get: function () { return fast_json_stable_stringify_1.default; } });

    Can be reproduced with Typescript v5.2.2, when compiling with --esModuleInterop.

  13. added
    Fix Available
    on Jan 29, 2024
  14. removed
    Needs Investigation
    on Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Fix AvailableRescheduled

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions