-
Notifications
You must be signed in to change notification settings - Fork 54
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
Add the types
condition to the package.json#exports
#60
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an improvement, but may still cause problems. The correct solution is to add an index.d.mts
file. See “Types are CJS, but implementation is ESM” at https://github.com/arethetypeswrong/arethetypeswrong.github.io.
I find it harder to reason when the {
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts"
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.ts"
"default": "./dist/index.js"
}
}
} I guess that this would be correct but it would come with a super small perf penalty as the runtime would have to resolve one more level of nesting. |
Yes, that would work. The perf penalty is negligible. |
Both files could have the very same content, right? They can't "proxy" to the same file though, IIUC. So this solution would require me to copy the |
Yes, or have the ESM types re-export from the CJS types. |
I pushed out what I believe is correct now. Would you mind taking another look at this @andrewbranch ? However, this particular module might still have problems. The generated
Note that A separate thing is that Vite is a module - that cannot be required but they provide a |
You should be able to use an import type— |
In this instance, it's quite problematic because this whole thing is generated by
This would be sweet |
When using the new `"moduleResolution": "bundler"` option in TypeScript 5 I came across an error with the type definitions: ``` Could not find a declaration file for module '@onsetsoftware/automerge-patcher'. '/Users/ben/dev/crdt-test/node_modules/@onsetsoftware/automerge-patcher/dist/automerge-patcher.es.js' implicitly has an 'any' type. There are types at '/Users/ben/dev/crdt-test/node_modules/@onsetsoftware/automerge-patcher/dist/types/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@onsetsoftware/automerge-patcher' library may need to update its package.json or typings.ts(7016) ``` To fix this I've updated package.json to correctly link the declaration files (as seen in a similar pull request here: gxmari007/vite-plugin-eslint#60 )
When using the new `"moduleResolution": "bundler"` option in TypeScript 5 I came across an error with the type definitions: ``` Could not find a declaration file for module '@onsetsoftware/automerge-patcher'. '/Users/ren/dev/crdt-test/node_modules/@onsetsoftware/automerge-patcher/dist/automerge-patcher.es.js' implicitly has an 'any' type. There are types at '/Users/ren/dev/crdt-test/node_modules/@onsetsoftware/automerge-patcher/dist/types/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@onsetsoftware/automerge-patcher' library may need to update its package.json or typings.ts(7016) ``` To fix this I've updated package.json to correctly link the declaration files (as seen in a similar pull request here: gxmari007/vite-plugin-eslint#60 )
@gxmari007 can you merge this PR to fix the error: |
Needed for newer typescript with certain moduleResolution settings. Found the explanation & solution here: gxmari007/vite-plugin-eslint#60 & the docs for the option here: https://nodejs.org/api/packages.html#packages_exports (with `types` being defined here: https://nodejs.org/api/packages.html#conditional-exports)
Needed for newer typescript with certain moduleResolution settings. Found the explanation & solution here: gxmari007/vite-plugin-eslint#60 & the docs for the option here: https://nodejs.org/api/packages.html#packages_exports (with `types` being defined here: https://nodejs.org/api/packages.html#conditional-exports)
Not the ideal end solution, but you can also just turn off typescript's enforcement of this in your tsconfig.json
Another option is you can use patch-package and just modify the package.json you want to add the exports/imports to and then apply the patch in your project. |
See TypeScript issue here: microsoft/TypeScript#52363 I don't entirely follow what TS is doing, but I've based this commit on a PR linked from that discussion: gxmari007/vite-plugin-eslint#60 I don't think this will work for CJS imports as TS seems to be pedantic around file suffixes -- we may need to copy or wrap the .d.ts file somehow. I'll investigate that separately.
Needed for newer typescript with certain moduleResolution settings. Found the explanation & solution here: gxmari007/vite-plugin-eslint#60 & the docs for the option here: https://nodejs.org/api/packages.html#packages_exports (with `types` being defined here: https://nodejs.org/api/packages.html#conditional-exports) Co-authored-by: Manu [tennox] <tennox+git@txlab.io>
This is required for TypeScript to work with packages that have
exports
when using the newmoduleResolution: node16/nodenext/bundler
settings. When those are not used then TypeScript just ignorespackage.json#exports
- but when you make it aware ofexports
by using those options then it requires thetypes
to be included inexports
and not as the top-level key ofpackage.json