esModuleInterop default import mapping for CommonJS modules with dual exports
0 reputation · 03 Oct 2020, 15:11 UTC
Goal
Achieve predictable default imports when migrating a small application to TypeScript without downtime, relying on esModuleInterop to synthesize default exports from existing CommonJS dependencies.
Constraints and uncertainty
The compiler maps a default import to module.exports when only that property is assigned, but to module.default when both module.exports and module.default exist. This heuristic is not codified in the language specification and may change between minor TypeScript releases. Runtime behavior also depends on how each CommonJS module mutates its exports after initial load, which static analysis cannot verify.
Questions
- Is there a documented or configurable rule that forces the default import to always resolve to
module.exportsregardless ofmodule.defaultpresence? - Can a project-wide compiler option or module augmentation guarantee consistent mapping across all third‑party CommonJS packages during a zero‑downtime migration?