-
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
Types that are found, but not exported in package.json, should still be usable #52363
Types that are found, but not exported in package.json, should still be usable #52363
Comments
Yes, but You can often debug such issues with
|
So is the error wrong then? The error is saying that the file just needs to be in exports, but really the issue is that the file has the wrong extension. |
The error is not wrong; it does not say that the file needs to be listed in
The hope was that folks would open issues, not just work around it for themselves 😕 |
Something that would be nice to have is a way for library authors to indicate that a CJS (In this case, it looks like no syntactic transformation is actually required, but I think in general you need to know whether to set the CJS module exports to the I’ll continue thinking about this, but I think the version of this option that you’re asking for is too loose. At the same time, so many libraries are misconfigured that everyone would turn it on, and we would forever lose the ability to do something more correct. In the meantime, please do let the library maintainers for @web/dev-server know that they are missing an |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This seems like a very prominent issues with many packages that I am trying to use in my project with Typescript v5.0-beta and https://github.com/gxmari007/vite-plugin-eslint |
Here you go, I fixed this for you: gxmari007/vite-plugin-eslint#60 |
That was fast. Thank you! |
If anyone's still paying attention to this issue... what's the fix when you encounter this error? I haven't been able to upgrade any of my projects to TypeScript 5.0 w/ resolution "bundler" yet because they have too many dependencies that trigger this error. The types are there, I just want to tell TypeScript to actually use them. |
File an issue or PR against the library. I built https://arethetypeswrong.github.io after closing this issue to help users convince library authors to fix their types. @web/dev-server was fixed in v0.1.36. |
And in the meantime - |
Thanks @Andarist , just learned about patch-package from you - what a game changer! 🥳 For this case make sure to include changes in npx patch-package insertPackageName --exclude 'nothing' |
|
Correctly exporting types in package.json. See issue and solution here microsoft/TypeScript#52363 for reference.
@andrewbranch I don't that's obvious to like 90% of Typescript developers and probably a large fraction of Typescript package maintainers. There are some incompatibilities around default exports but I thought that's what the tsconfig esModuleInterop option was for? I think most of us haven't thought further than that. What else is different between the typings for CJS and ESM? |
Oh, this is definitely unintuitive. When I said “obviously” in that message, what I meant was that it’s unambiguous and inflexible to tooling like Node and tsc due to file extensions and package.json
The other significant difference between ES modules and CJS modules as far as TypeScript is concerned (that is, things that would make a difference in how declaration files are processed) is the module resolution algorithm is different, so tsc needs to know which algorithm to use (again on a file-by-file basis—actually, on an import-by-import basis, since you can have a ESM dyanmic import in a CJS file or a CJS I have some new module docs in progress at #52593 and an appendix on ESM/CJS interop is already done if you want to dive into more details: https://gist.github.com/andrewbranch/79f872a8b9f0507c9c5f2641cfb3efa6#file-04_01_esm-cjs-interop-md Also, for package authors, I recently invested a lot into the docs for https://arethetypeswrong.github.io, so if an author tries to use a |
Fix: ``` Could not find a declaration file for module 'add-to-calendar-button-react'. 'node_modules/add-to-calendar-button-react/dist/atcb.js' implicitly has an 'any' type. There are types at 'node_modules/add-to-calendar-button-react/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'add-to-calendar-button-react' library may need to update its package.json or typings. import { AddToCalendarButton } from "add-to-calendar-button-react" ``` I checked the related issues from this main issue and the common way to fix it, is by exporting the types as well: microsoft/TypeScript#52363
What a complete mess. I am so tired of typescript breaking things and then blaming package authors for "doing it wrong." If new version typescript can't figure out how to apply types to .cjs and .mjs for 95% of packages then that is the fault of typescript. The typing should be agnostic by default, or the 5% edge cases should provide clearer types for their obscure use of imports. Having to use patch libraries or copy/paste type declarations is a joke. Sticking to 4.9.4 and |
Totally agree. If 95% of packages had this problem, that would force us to consider a different design. Luckily, I have the numbers, and it’s 11% of types-shipping packages (area of red + green here; y-axis is percentage expressed as a ratio; sampled from thousands of the most downloaded / depended upon packages): I don’t want anyone to get the impression that TS is blaming package authors. I explained once:
I’m not very interested in assigning blame, but if I have to, we dropped the ball on communicating how this was going to work early enough and clearly enough. We’re all on the same side though; everyone wants stuff to work perfectly for a wide variety of users in a huge matrix of different scenarios. I have thought a lot about how we could do things differently today to make that easier, and better tools + docs that explain how things work today was the top priority. Unfortunately, there’s no magic wand we can wave over the TypeScript codebase and make all the existing typed npm packages work perfectly. We just have to do better about helping library authors make valid ones in the future, and help chip away at the ones that already have problems. |
Isn't this issue in regards to packages that provide I realize now that I am forced to fix this issue at metaphorical gun-point, because nextjs (and I guess vue) templates are shipping with this new version by default. So I have to do this or else all typing with my packages break with this new update. Great. For anyone else reading, I resolved the issue by replacing my
I still don't understand the purpose of any of these breaking changes, why the exports format has to change yet again, or why I'm forced to make this change when it was working perfectly fine before. |
That
Here are some resources. |
Suggestion
With module resolution "Node16" or "NodeNext" I'm running into problems using packages where tsc can find declaration files but won't use them because they're not exported in package.json.
The error I get is:
This means that I can't use the package without copying it's types or patching the package locally with something like patch-package.
I know the package should technically be exporting its types correctly (though I might argue that this is being overly pedantic and that the existence of the these specific files strongly implies that the author intended them to be used), but as a user of TypeScript I'm stuck and tsc seems to have enough information to just laod the types for me so I can at least make progress.
I'd like some sort of option to tell tsc to loosen up about these files. If there's an encapsulation violation or something breaks with the next version of the package I'm using, I'll deal with it.
It's also possible that behavior is a bug. As commented on by @andrewbranch in #46334:
This seems to say that if I'm importing
index.mjs
and there's anindex.d.ts
next to it, the types should load. Is that correct?🔍 Search Terms
A lot of issues came up in the first search. I could have missed a duplicate.
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
An option to load types not exported in packae.json.
📃 Motivating Example
https://github.com/modernweb-dev/web/blob/master/packages/dev-server/package.json
💻 Use Cases
Packages that aren't ready for TypeScript's strict type loading behavior.
The text was updated successfully, but these errors were encountered: