Repository navigation
Hoisted function declarations should be assigned to exports before any require calls #39853
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issueHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosThis issue has compiler-backed repros: https://aka.ms/ts-reprosRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Milestone
Description
Activity
- addedRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
on Dec 11, 2020 - addedHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosThis issue has compiler-backed repros: https://aka.ms/ts-repros
on Feb 11, 2021 typescript-bot commented
on Apr 13, 2022 ContributorMore actions👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.
Issue body code block by ExE Boss (@ExE-Boss)
👍 Compiled
Emit:"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.b = void 0; const a_js_1 = require("./a.js"); // Crashes when transpiled by TypeScript: (0, a_js_1.a)(); function b() { return 123; } exports.b = b;
Historical Information
Version Reproduction Outputs 4.4.2, 4.5.2, 4.6.2 👍 Compiled
Emit:"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.b = void 0; const a_js_1 = require("./a.js"); // Crashes when transpiled by TypeScript: (0, a_js_1.a)(); function b() { return 123; } exports.b = b;
4.2.2, 4.3.2 👍 Compiled
Emit:"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.b = void 0; const a_js_1 = require("./a.js"); // Crashes when transpiled by TypeScript: a_js_1.a(); function b() { return 123; } exports.b = b;
- addedFix AvailableA PR has been opened for this issueA PR has been opened for this issue
on Mar 6, 2024 - locked as resolved and limited conversation to collaborators
on Oct 21, 2025
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issueHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosThis issue has compiler-backed repros: https://aka.ms/ts-reprosRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
TypeScript Version: 4.2.0-beta
Search Terms:
Code
Workbench Repro
Expected behavior:
The
exports.*assignment should be done immediately after__esModule, just like how Babel does it.Actual behavior:
The exports assignment is performed after the function declaration, which causes behaviour differences between native ES modules (where hoisted function declarations are initialised before any code begins executing) and transpiled CommonJS modules (where hoisted function declarations are added to the
exportsobject afterrequire(…)calls)Playground Link: a.js, b.js
Related Issues: #37093