microsoft / microsoft/TypeScript

`moduleResolution: bundler` diverges from bundlers following node semantics in regards to `default` import

Open
#54,102 11 comments 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

moduleResolution bundler node webpack default namespace

🕗 Version & Regression Information
  • This is the behavior in every version I tried: 5.0.4 (the only one with moduleResolution: bundler
⏯ Playground Link

Repro case can be found here

🙁 Actual behavior

When resolving a .d.mts file that proxies to .d.ts in a CJS package with moduleResolution: bundler, TypeScript complains with an error like:

Property 'default' does not exist on type 'string'.ts(2339)

The reason that it complains about it is that TypeScript is resolving default import as if the .d.ts in this CJS package could export the "real" default export. webpack actually follows semantics coined by node and it loads the namespace object as the default export of that "cjs file".

So the type-level reality diverges there from runtime. At runtime, with webpack - it works exactly like it would work in node. TypeScript thinks that the intention was to load module.exports.default there though.

🙂 Expected behavior

I'm not exactly sure what's the expected behavior here because "resolving" like a bundler is quite under-specified and bundlers are not quite consistent when it comes to this. Even when we check this very same repro, we can see that I had to use defaultIsModuleExports: true in Rollup for it to behave the same. By default, Rollup behaves in the same way as TypeScript does.

cc @andrewbranch

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

Start by running the linked repro and tracing resolution from the .d.mts proxy to the .d.ts file under moduleResolution: bundler. Compare the observed TypeScript result with webpack, Node semantics, and Rollup's documented behavior. Done requires an agreed expected behavior and a regression case, but the issue does not identify the relevant source or test files.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, rollup, typescript, webpack
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.