-
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
Different behavior for type reference directive in nodenext based on whether types are in @types or not #47444
Comments
Note: there are quite possibly differences between |
I agree that they should definitely behave the same - I image we probably need to pass forward the settings used for |
@weswigham I’m coming back around to this, but I feel like your assessment above may have changed in light of #47732. I feel like it would make sense to base the resolution mode of automatic type reference directives on the package.json |
Further, it seems odd to me that triple-slash references, regardless of requesting resolution mode, wouldn’t use You might say that a triple-slash reference isn’t an import, so it doesn’t seem obvious that it should care about exports. But triple-slash references have always pulled from the |
Yeah, the important note was consistency in resolution between the two scopes - it's certainly more likely useful to always follow exports/other node style resolution rules for triple slash refs. (Though it'd probably annoy some people who've repurposed them over at deno :) ) It should definitely remain independent of the project's |
Wait, why should it be independent of |
I'm thinking we should probably have triple-slash refs always use, eg, Also, the secondary lookup does use the features passed in - but type reference resolution already always passes in a fixed feature set, regardless of compiler options. |
I don’t think so TypeScript/src/compiler/moduleNameResolver.ts Line 359 in 1592210
|
Riiiight, I changed it from always-cjs to option-dependent when I added the mode flags... It should probably be changed to always-latest-node (which I mentioned as an option in that code comment near where you linked). The further proliferation of resolution settings makes me seriously want to avoid having a lot of variants of how TS resolves triple-slash refs just because we're forced to have a lot of variants for imports. Since only we read them, we can choose just one standard resolution scheme - it doesn't need to be configurable. |
I think that seems reasonable, but do you still want to allow CJS/ESM mode overrides? If you want to do |
Yeah, I think the mode override is meaningful for pulling in augmenting modules only accessible under import/require conditions, and the conditions should probably be the same |
For the proposed |
I don't think the condition TS uses should depend on the resolution mode of the project - it'd make using the same declaration file in a minimal, node, and node16 project pretty hard. Given that, always doing the nodenext mode calculation seems like the way to go, insofar as it probably minimizes the need for mode assertions. |
But in those other modes, package.json |
Bug Report
🔎 Search Terms
type reference directive node12 nodenext
💻 Fourslash server test case
I used fourslash server tests to verify this because it treats files in node_modules as a real tsc run would, whereas the compiler test suite treats them as if they’re root files for compilation, which messes up what I’m trying to demonstrate.
I have two dependencies in node_modules here that are structurally identical, but one is inside
@types
and the other is not. The one in@types
is resolved by theprimaryLookup
ofresolveTypeReferenceDirective
in moduleNameResolver.ts, whichtypeRoots
(including@types
).ts
and.d.ts
files (not the cts/mts variants)exports
The one outside of
@types
, by merit of not being intypeRoots
, uses thesecondaryLookup
function, which uses a very different algorithm that brings in some Node12/NodeNext resolution features. Itnode_modules/*
first before tryingnode_modules/@types/*
exports
with conditionsnode, require, types
This test case shows how this discrepancy can make a difference in resolution based only on whether the package is inside
typeRoots
or not.I don’t know what the expected behavior is here, but it feels like it should probably be the same whether the package is in
@types
or regularnode_modules
.🙁 Actual behavior
exportsInsideAtTypes
andtypesFieldOutsideAtTypes
are unresolved (meaning inside@types
resolves to thetypes
field while outside@types
resolves to theexports
field)🙂 Expected behavior
Either both
typesFieldInsideAtTypes
andtypesFieldOutsideAtTypes
or bothexportsInsideAtTypes
andexportsOutsideAtTypes
should be unresolved, while the other pair is resolved.(@DanielRosenwasser @weswigham)
The text was updated successfully, but these errors were encountered: