-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Incorrect support of exports
in package.json
when generating CommonJS files
#54263
Comments
TypeScript won't understand that you have the You are telling it to always use
|
@Andarist I understand what you are saying. But it’s not sensitive to use the Plus I’m not those package maintainers. There are dozens out there that have similar looking |
Maybe the simpler choice would be to remove ts1479 altogether from the codebase. It’s not very useful to check this in TS since node do at runtime. It’s always nice to have more checks to prevent mistakes. But maybe this time it’s a little too involved a check to make? |
I'm afraid that's not exactly true. Some things might actually be allowed in one module system while not being allowed in the other one. For example, this is an error in the ESM file because it corresponds to // @moduleResolution: node16
// @module: esnext
export = number There are probably more subtle differences that I can't list on the spot from the top of my head. Andrew probably could elaborate on this but the bottom line is that - the module format is important for the declaration files.
I hear you here. People jumped the gun on adding I hate the current situation as well but it originates outside of TypeScript. TypeScript just adheres here to rules created by other standards and runtimes. It has to offer certain flexibility to make valid runtime setups typeable. |
There's a practical upper limit on how much we can choose to ignore manifest misconfigurations, of which this is definitely one. The real problem here is that all of this behavior is transitive, and there's also no guarantee that the types returned by the |
What you are telling me is new to me. I don’t understand much how this can be possible. This is really weird in the sense that you can only specify one It still doesn’t make any sense to me. I’m still thinking that this error should try to find a valide JS file using the same algorithm node uses, ignoring the typings for this. Thanks @Andarist and @RyanCavanaugh for your involvment in this though. |
And after thinking this through, it also seems to me that it means it’s impossible to write properly a I thought about adding a Wouldn’t it more sensitive to assume that the types file is compatible with everything that is exported? It’s sensitive to think that if the same module is exported both for CJS and ESM with the same |
This is totally valid and possible: {
exports: {
import: { types, default },
require: { types, default }
}
} On top of that, you could have lots of nested conditions like: {
exports: {
development: {
browser: {
import: { types, default },
},
},
},
} As you can see, there is truly no 1-to-1 mapping between the files targeting runtime and type-level. You can mix and match this however you want and it's all order-dependent. For example, you could move the types to the front in the example above: {
exports: {
types,
development: {
browser: {
import,
},
},
},
} Also, note that the
It's also not that simple because some things might be nominal - like classes with private fields. This could affect narrowing through |
@Andarist oops, I suppose I was unclear about my interrogations. I was talking about keeping support for And even in |
No, if your package has
Ye, this is also a little bit weird to me that all other runtimes made
Yes, kinda - the problem here is that with such a setting and a dual package the runtime world doesn't really reflect the type-world and the whole thing is in some weirdo, hard-to-reason-about, state. From TypeScript PoV, with |
Strange, I'm pretty sure I've ran into issues when keeping it in this mode. One last point before I close this, it means you can’t really use this syntax then: {
exports: {
types,
...
},
} It’s presented everywhere in the doc and it fails. Why not make it so that it means : "I assume the fact that this type file is correct for all type of packages in the following conditions and assume the responsibility if it does not"? It would be so much easier to work with… |
It's generally impossible for a single type file to work correctly with both ESM and CJS, because the semantics of how exports work differ: CJS actually just exports a single value (i.e. what |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
13 similar comments
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@fatcerberus It's easy when setting |
A comment not related to TypeScript itself, but just for someone who will be trying to use I had the same problem and the error message was:
In order to deal with this, I had to add the following lines to my "compilerOptions": {
"module": "CommonJS",
"moduleResolution": "Node"
}, |
Well, using |
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
💻 Code
Very simple codebase:
Using the following
tsconfig.json
:🙁 Actual behavior
Typescript generates the following error message:
🙂 Expected behavior
This should work. The important parts of the
package.json
file are:Here is the reference
package.json
.Even though the root directory of this file is marked as
"type": "module"
, it has arequire
exports. And thisrequire
exports point to a file in a directory containing apackage.json
looking like:Thus, it is a perfectly valid module to be
require
d.I’m pretty sure this should work.
node
requires this module perfectly well without giving an error.Note: remove
"type": "module",
from thepackage.json
using a patch fix the issue, but it is a real bother to maintain a patch for each module using this structure (there are a lot more out there).The text was updated successfully, but these errors were encountered: