fix: add compatibility for modern module resolutions #157
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Defines the correct typings file in the
types
field, and adds the same field inside theexports
for correct TypeScript module resolution support.Types of changes
Description
This PR solves the type resolution for this package, previously it would fail and instead, TypeScript would try to load (or rather, infer) the types from the
./dist/module.mjs
file rather than loading thetypes
property inpackage.json
, this is, after all, because theexports
field overrides it.This field is also order-sensitive, TypeScript will try to load the first condition on the object (the properties are, after all, conditions that are tested against in insertion order). This will not change the behavior when loading the package from Node.js, as
types
is not a valid condition for the runtime. You can see a PR with a detailed description here.I have run
yarn patch
with this exact change and after it, Nuxt to properly find the type. This PR mirrors said patch. If replicating this issue comes out complicated, I believe you may need to settypescript.tsConfig.compilerOptions.moduleResolution
to'bundler'
(alternativelynode16
ornodenext
) in yourdefineNuxtConfig
so TypeScript loads theexports
field rather than trying the top-level properties. This is tested with TypeScript v5 (the latest stable as of writing).Without this PR, Nuxt cannot infer the type from the default export, making the
security
property be typed asRecord<string, any>
rather thanModuleOptions
, as seen from theNuxtConfig
declaration:Importing the types from
module.d.ts
rather thantypes.d.ts
was also a deliberate choice, as of0.13.0
, thetypes.d.ts
file doesn't allow importing other types (such asModuleOptions
) as it restricts the exports to the default export, which may be undesirable:Checklist:
Not applicable since it affects how module resolution works and does not change the library's behavior.