-
-
Notifications
You must be signed in to change notification settings - Fork 280
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
fix(all): add types to exports for node16/nodenext module resolution #2094
base: master
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: e3dff29 The changes in this PR will be included in the next version bump. This PR includes changesets to release 30 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
"import": "./index.mjs", | ||
"require": "./dist/index.js" | ||
"import": { | ||
"types": "./index.d.ts", |
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.
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.
That would work for me, but it would break all packages in typescript when not using node16/nodenext module resolution, because in that mode typescript only looks for .d.ts
files.
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.
That shouldn't be true. My understanding, reiterated in the issue I liked to, is that TypeScript only looks for .d.mts
declarations files for .mjs
files, and .d.cts
declaration files for .cjs
files. IOW, the declaration file must be named after the file its describing always, and there wasn't event a version of TypeScript that even supported .mjs
files with .d.ts
declarations.
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.
You're correct, but that doesn't stop it from being broken. If typescript isn't configured to use Node16/NodeNext resolution, typescript looks for the d.ts
file extension but it doesn't look for .d.mts
or .d.cts
file extensions. That makes sense as Node doesn't autocomplete the .cjs
or .mjs
extensions either.
The @web/dev-server-core
package has an export ./test-helpers
. To support this export in non-Node16/NodeNext resolutions the package must have a ./test-helpers.d.ts
file.
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.
To support this export in non-Node16/NodeNext resolutions the package must have a ./test-helpers.d.ts file.
You’re referring to resolution in the TypeScript mode called node
, which in 5.0 has been renamed to node10
to reflect what it really is. Node 10 will fail at runtime on an import of "@web/dev-server-core/test-helpers"
, so putting a .d.ts
file at ./test-helpers.d.ts
to make TypeScript think it will resolve is strictly wrong. Users who are running on a Node version newer than v10 should not be using that setting anymore.
Of course, taking my pedant hat off, users are still using that setting even though the vast majority of them are no longer using Node 10. It’s up to you to decide how and if you want to protect those users from their own misconfiguration. Placing the phantom .d.ts
file there works. Other strategies are demonstrated at https://github.com/andrewbranch/example-subpath-exports-ts-compat. Particularly interesting is that you can use typesVersions
to affect TypeScript node10
resolution almost exactly the same way that exports
affects the modern TypeScript resolution modes.
Whatever strategy you decide to go with, you can use https://arethetypeswrong.github.io to test it. I can confirm that the issue pointed out here by @justinfagnani is indeed a problem that will be recognized by this tool.
Add types in exports. This was done as type script started to show errors. ref: modernweb-dev/web#2094 microsoft/TypeScript#52363
Add types to exports for typescript's node16 module resolution.
Two packages were already fixed in #1973 and #1981, this PR fixes the rest.
I automated this change using
jq
to prevent typos or other small mistakes in this repetitive task.jq script