Repository navigation
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
mxmauro commentedon Sep 5, 2020
Here the same, cannot get rid of
__importDefautmrttrifork commentedon Nov 23, 2020
I see that 4.1.2 is released. Please consider to include this issue in 4.2
mrttrifork commentedon Mar 16, 2021
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 functionWhere noop.ts looks like this
export function noop() { /* Do nothing */ }This way i get __exportStar imported and then everything works as intended
6 remaining items
VitorLuizC commentedon Feb 14, 2022
Still happening in TypeScript
4.5.5.piyushkmr commentedon Apr 28, 2022
Still happening with
4.6.3How 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.thynson commentedon Jun 6, 2023
Still happens in Typescript 5.1.0
thynson commentedon Jun 7, 2023
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.
Shamilik commentedon Sep 25, 2023
Got into same problem on 4.9.5 with
fixed with
thynson commentedon Sep 26, 2023
Can be reproduced with Typescript v5.2.2, when compiling with
--esModuleInterop.