microsoft / microsoft/TypeScript

"export = { }" vs "let a = { }; export = a", causes discrepancy in import behavior

Open
#39,152 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.9.5

Search Terms: I searched import related bug labels

Code

//module a
export = {
  name: 'zzxoto'
};

//module b
const EX = { name: 'zzxoto' };
export = EX;



//////////////////////////////////////////////////////////////////
//when importing a

//this gives error:
//This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.ts(2497)
import { name } from './a';

//this works
import A from './a';
A.name;
//////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////
//when importing b

//this works
import { name } from './b';

//this works too
import B from './b';
B.name
//////////////////////////////////////////////////////////////////


//tsconfig.json
{
  "compilerOptions": {
    "target": "ES2019",
    "module": "commonjs",
    "strict": false,
    "strictNullChecks": true,
    "allowJs": false,    
    "esModuleInterop": true
  },
  "include": ["**/*.ts"]
}

Expected behavior:
There should be no descrepancy while import module a vs module b.

Actual behavior:
Described above.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the discrepancy using modules a and b from the report with TypeScript 3.9.5 and the provided tsconfig.json. Compare named and default imports under esModuleInterop, then verify that equivalent export forms produce consistent import behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.