-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Module resolution: resolution-mode="require"
emitted too eagerly
#56592
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
Comments
From #55579:
This is my best guess, which means it’s working as intended, or at least, working as somebody intended. I’m not sure if that behavior is ideal. Do you have any real examples of it being incorrect? |
I'm not super sure how those triple slash directives work and when they are exactly needed etc - I almost never write them by hand. So take my opinion with a grain of salt etc. The quoted statement reads to me a little bit like "we need to inline type aliases to increase the likelihood that the functions declared by the module keep accepting arguments of the exact same shape as they did when it was created by the library's compilation". However, node types live kinda outside of the regular semver ranges governed by the dependency ranges... so I'm not sure. Maybe this is just a quirk of that? Are triple slash directives ever included in other scenarios (beyond when the library depends on some global-like declarations etc)? But even with that in mind... the node types development is usually additive. They are often installed by the consuming project - acting like a peer dep of sorts. So I think, I would still expect the behavior as close as possible to the one that was authored. If the source code goes through the import resolver then I think it makes total sense for the output declarations to go through the same import resolver too. This semantic change~ of the output is surprising. That being said - I didn't really notice this thing while investigating a broken case per se. I just noticed this because I tried to consume this package using TS 4.9 with As long as this extra attribute is not needed - I think it's not worth adding it since it has a real chance of breaking some builds. TS 5.3 permits this in all module resolution modes but this is a very recent change and IIRC, for example, types in DefinitelyTyped support at least 2 years of the last TS releases. I figured out it's something worth raising - even if only for this situation to findable by others in the issue tracker. |
They’re only emitted when the file references a global, ambient module, or exports from a module augmentation. The rationale behind including the |
Isn't at least this bit indicating that this issue here has some merit? Scenario:
If those "redundant" I have a massive headache and thinking about modules doesn't soothe it 😅 I understand that there are just tradeoffs to be considered here, for almost every potential solution. Feel free to close this issue. |
For one, Wesley’s assertion is that when It’s extremely hard to argue that this behavior is right or wrong, given that global declarations and ambient module declarations kind of sit outside the ESM/CJS world. Like, what is the actual scenario we think might happen where the resolution of this triple-slash reference would change? |
// @module: Node16 /// import { URL } from 'node:url'; export const stuff: [URL?, EventEmitter?, Buffer?] = [new URL('https://www.typescriptlang.org/play'), /* Add a comma here */]; There is a syntax error in the code you provided. The issue is in the line where you initialize the stuff array. There's a missing comma after the first element |
Demo Repo
https://github.com./jaredwray/cacheable/tree/d7ac062f77bf8188221f914fd753122fd5aa5f65/packages/request
Which of the following problems are you reporting?
Something else more complicated which I'll explain in more detail
Demonstrate the defect described above with a code sample.
Input
emitted DTS
Run
tsc --showConfig
and paste its output hereimporting module
Run
tsc --traceResolution
and paste its output hereI think it would obscure this repro case - but I can add this information if requested.
Paste the
package.json
of the importing module, if it existsPaste the
package.json
of the target module, if it existsAny other comments can go here
I included a dynamic import to this ESM-only package in my CJS module
that generated its bit of a declaration file:
Overall, I understand that this is no longer a problem:
since TS 5.3:
However, I don't understand why
resolution-mode="require"
was added there in the first place. That file was ESM and I imagine that it should go through its default resolution mode (import?) when referring to node builtin modules. After all, the runtime output looks roughly equivalent to smth like this:Since this isn't a CJS module I think that its DTS output shouldn't try to "dereference" those references but rather should attempt to preserve the import style~ of the source.
cc @andrewbranch
The text was updated successfully, but these errors were encountered: