-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
TypeScript generated invalid types if esModuleInterop
is enabled
#52779
Comments
This is correct only if type checking assumes that the target module has a synthetic default (i.e., does not have the |
Once this problem exists in one library, this is likely to “infect” all its dependants too. I think this is partly because TypeScript gives improper hints on how to fix it. Let’s say someone writes a library
As stated in the OP, the correct fix for this is to use either If the author of The consumers of
I’m not familiar with the details from #47947, but it introduces two new compiler options. As far as I understand, it makes sense to make those options mutually exclusive with |
I discussed another solution to this in #54212 |
Bug Report
When a package is typed using
module.exports =
, the correct way to type it isexport =
. The correct way to import this, in either.ts
or.d.ts
files, isimport identifier = require('module')
. For exampleimport React = require('react')
.If a user enables
esModuleInterop
, they are allowed to useimport React from 'react'
. When compiling to CJS, this will output JavaScript that used eithermodule.exports.default
ormodule.exports
.IMO This isn’t great, but it works.
What is bad, is that it generates a default import in the type definitions.
Whereas it should be typed as this:
If this is library code, that means the user of the library now is also enforced to use
esModuleInterop
, whether they like it or not.🔎 Search Terms
esModuleInterop
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs
⏯ Playground Link
Playground link with relevant code (Look at the
.d.ts
output)💻 Code
The following code requires
esModuleInterop
to be enabled.🙁 Actual behavior
This is the generated type definition:
🙂 Expected behavior
This is the correct type definition:
The text was updated successfully, but these errors were encountered: