Skip to content
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

Export the types file #721

Merged
merged 2 commits into from
Mar 19, 2024
Merged

Export the types file #721

merged 2 commits into from
Mar 19, 2024

Conversation

simon-tma
Copy link
Contributor

If there is an exports key set, Typescript requires the types file to be included in the list of the files exported.

Without this change, Typescript errors when trying to import types with:

error TS7016: Could not find a declaration file for module 'ziggy-js'. '/.../node_modules/ziggy-js/dist/index.js' implicitly has an 'any' type.
  There are types at '/.../node_modules/ziggy-js/src/js/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'ziggy-js' library may need to update its package.json or typings.

If there is an `exports` key set, Typescript requires the types file to be included in the list of the files exported.
@Mohammad-Alavi

This comment was marked as off-topic.

@bakerkretzmar bakerkretzmar self-assigned this Mar 19, 2024
@bakerkretzmar
Copy link
Collaborator

@simon-tma what version of TypeScript are you using? Can you link me to where TypeScript says types is required (I can see that it's available, but it looks optional). Happy to add this if it's necessary but the TS docs make it seem like everything should work fine either way.

@simon-tma
Copy link
Contributor Author

@bakerkretzmar After further digging (using https://www.typescriptlang.org/docs/handbook/modules/theory.html#module-resolution, microsoft/TypeScript#52363 and https://arethetypeswrong.github.io/?p=ziggy-js%402.0.4), it's not that types is required in exports, it's that the top-level one is ignored when doing resolution using exports so typescript falls back to looking for sibling files instead.

Resolution trace from "Are The Types Wrong" showing what paths are being searched for the declaration file:
Screenshot 2024-03-20 at 08 57 25

I'm seeing this with TS 5.3 and 5.4, though it's likely to happen with any version of Typescript using nodenext or node16 for module resolution.

@bakerkretzmar
Copy link
Collaborator

@simon-tma gotcha, makes sense. Appreciate you sharing those details, and that "are the types wrong" tool is very cool.

@bakerkretzmar bakerkretzmar merged commit f0b7a36 into tighten:2.x Mar 19, 2024
16 checks passed
@bakerkretzmar
Copy link
Collaborator

Released just now in v2.0.5. Thanks again for digging into this 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants