Skip to content

Fix esModuleInterop not being respected for JS CommonJS imports#2693

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-tsgo-esmodule-interop-issue
Draft

Fix esModuleInterop not being respected for JS CommonJS imports#2693
Copilot wants to merge 2 commits intomainfrom
copilot/fix-tsgo-esmodule-interop-issue

Conversation

Copy link
Contributor

Copilot AI commented Feb 5, 2026

tsgo reports TS2497 when importing named exports from CommonJS JavaScript files even when esModuleInterop: true is set. tsc allows these imports without error.

// tsconfig.json: { "allowJs": true, "esModuleInterop": true }

// shared.vars.js
module.exports = { foo };

// index.ts - tsgo incorrectly errors here with TS2497
import { foo } from "./shared.vars";

Root Cause

The Go binder creates an export= symbol with SymbolFlagsProperty for module.exports = { ... }, while TypeScript exports each shorthand property directly. The property symbol lacks SymbolFlagsModule | SymbolFlagsVariable, triggering the interop error check in resolveESModuleSymbol.

Changes

  • internal/checker/checker.go: Skip TS2497 for JavaScript files when ESModuleInterop.IsTrue() (uses tristate, not the deprecated always-true GetESModuleInterop())
  • testdata/tests/cases/compiler/esModuleInteropJsCommonJsImport.ts: Test case reproducing the issue
Original prompt

This section details on the original issue you should resolve

<issue_title>tsgo does not respect esModuleInterop in certain cases</issue_title>
<issue_description>## Steps to reproduce

When importing named exports from a CommonJS JavaScript file (with allowJs: true), tsgo reports TS2497 even though esModuleInterop: true is set in tsconfig.json.

tsc allows this import without error.

Reproduction: https://github.com/psm14/tsgo-esmodule-interop-reproduction

Behavior with typescript@5.9

❯ npm run build:tsc

> tsgo-esmodule-interop-repro@1.0.0 build:tsc
> tsc --noEmit

Behavior with tsgo

❯ npm run build:tsgo

> tsgo-esmodule-interop-repro@1.0.0 build:tsgo
> tsgo --noEmit

src/index.ts:3:32 - error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.

3 import { helper, format } from './utils';
                                 ~~~~~~~~~


Found 1 error in src/index.ts:3
```</issue_description>

<agent_instructions>Create a test case lot of the linked repo that shows the bug, and commit that. Then fix the issue.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@jakebailey</author><body>
Feels like https://github.com/microsoft/typescript-go/issues/1054, potentially </body></comment_new>
</comments>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

For JavaScript CommonJS modules with `module.exports = { ... }`,
the binder creates an export= symbol with SymbolFlagsProperty, not
SymbolFlagsValueModule. This was causing TS2497 to be emitted even
when esModuleInterop was enabled.

The fix adds a check in resolveESModuleSymbol to skip the error for
JavaScript files when esModuleInterop is explicitly enabled.

Fixes #1124 and related issue #1054

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix tsgo not respecting esModuleInterop in certain cases Fix esModuleInterop not being respected for JS CommonJS imports Feb 5, 2026
Copilot AI requested a review from jakebailey February 5, 2026 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tsgo does not respect esModuleInterop in certain cases

2 participants