-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Runtime error with *esModuleInterop* when CJS module has an export named "default" #38540
Comments
FindingsI have already spent quite a lot of time debugging the issue, here's what I found. The issue only arises when the The actual problem is inside the That one is defined as: var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
}); Now if the module already has an export called ProposalIn my view, While this is somewhat of a compromise:
I'll be happy to submit a PR as soon as this is accepted. Working branch: https://github.com/marcelltoth/TypeScript/tree/fix/safe-set-module-default |
Same issue here using Typescript v3.9.2 with Sequelize v5.21.8, which uses a "default" on their exports, which is causing TS to choke on: https://github.com/sequelize/sequelize/blob/master/lib/sequelize.js#L1356 Reverting back to v3.8.3 fixes this for us as an interim solution. |
* feat: ui-kit table of contents upgrade * fix: workaround TS bug microsoft/TypeScript#38540
This is unfortunate since the promise.all fixes in 3.9 we have been waiting on for a long time now. Are there any suggested workarounds (short of downgrading) that would allow us to move to 3.9 without getting bit by this (we are hitting it with axios and some other libraries as well) |
I can confirm having this same issue using TS 3.9.2, so I'll also be reverting all my work to v3.8.3 till this closes |
@devshorts What I ended up with is to split my imports in a way so the compiler emits an Axios for example does not export any constant but the default, all the rest are just types so you can do: // instead of
import axios, { SomeAxiosType } from 'axios';
// do this
import axios from 'axios';
import type { SomeAxiosType } from 'axios'; But I'll submit a fix as soon as some maintainer approves this issue. |
+1 |
+1 I managed to reduce it to a minimum jest test case that fails:
With the output:
(Yes, the line number are wrong, I don't know why. The source file is just 6 lines.) |
Looks like tslib had the same issue: microsoft/tslib#102 They fixed it by reverting to the previous behavior in tslib 1.x, and publishing the breaking change as 2.0. This means that another workaround for this issue is to add tslib ^1.13.0 as a dependency, then set If this is working as intended, I hope that it can be reverted in 3.x and pushed back to 4.0, since it's a breaking change. |
We're also running into this with the launchdarkly react-client-sdk. See launchdarkly/react-client-sdk#36. Here is a another simple demonstration of the issue: https://github.com/joeldenning/typescript-esm-cjs-interop |
We should have a fix at #38808. If you can pick up the build that was produced here and try it out, that'd really help give us some confidence that we can back-port it to 3.9. |
@DanielRosenwasser That build fixes the issue for me. Modules imported with the |
The fix for this is released in 3.9.4, which is currently released to npm under the |
TypeScript Version: 3.9.2 & 4.0.0-dev.20200512
Search Terms: __setModuleDefault, __importStar, redefine, ts-lib
Code
Check out the full example at https://github.com/marcelltoth/typescript-bug
File
dangerous-module.js
(commonjs module)This is a pattern seen in real world, when the authors of CJS modules are trying to be nice with us TypeScript users. This way the module is nicely consumable from TS without esModuleInterop. The pattern is used by - for example - axios
I have esModuleInterop and therefore allowSyntheticDefaultImports turned on. Then in another file:
File
index.ts
I build it via
tsc
, then runnode dist/index.js
on the output.Expected behavior:
The code logs to the console.
Actual behavior:
It throws on the import line like so:
Playground Link: https://github.com/marcelltoth/typescript-bug
Related Issues: #37113
The text was updated successfully, but these errors were encountered: