There has been a few issues in the past about TypeScript having problems generating correct results around CJS and default export. But those issues focus on certain aspect of the problem and I couldn't find one that clearly show how big an issue it is.
So I decided to give it a shot and it seems to be worse than I thought.
Here are a few TL;DRs:
When you use export default
in your code:
- The declaration for CJS is incorrect.
When you consume a package that uses export default
and transpiled to CJS:
module: ES* or Node*
does not produce correct code (runtime error)
When using moduleResolution: Node16 or NodeNext
:
- You cannot use
allowSyntheticDefaultImports
oresModuleInterop
, which you must use inNode
for packages withexport =
Related issues:
microsoft/TypeScript#50501 microsoft/TypeScript#50152 microsoft/TypeScript#41898
- This is a crash
- This changed between versions ______ and _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
Playground link with relevant code
// We can quickly address your report if:
// - The code sample is short. Nearly all TypeScript bugs can be demonstrated in 20-30 lines of code!
// - It doesn't use external libraries. These are often issues with the type definitions rather than TypeScript bugs.
// - The incorrectness of the behavior is readily apparent from reading the sample.
// Reports are slower to investigate if:
// - We have to pare too much extraneous code.
// - We have to clone a large repo and validate that the problem isn't elsewhere.
// - The sample is confusing or doesn't clearly demonstrate what's wrong.