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

Fix exports to work with TS with both ESM and CJS #177

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

duniul
Copy link

@duniul duniul commented Sep 19, 2023

Fixes #176

Context

See #176, but it's currently not possible to import better-ajv-errors with some TypeScript configurations because of the exports definition in package.json.

This PR follows the recommendations from https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing to fix the issue.

Changes

  • Copy typings file to unique files in each built folder
    • Required by TS, this is how they explain it:

      It’s important to note that the CommonJS entrypoint and the ES module entrypoint each needs its own declaration file, even if the contents are the same between them. Every declaration file is interpreted either as a CommonJS module or as an ES module, based on its file extension and the "type" field of the package.json, and this detected module kind must match the module kind that Node will detect for the corresponding JavaScript file for type checking to be correct. Attempting to use a single .d.ts file to type both an ES module entrypoint and a CommonJS entrypoint will cause TypeScript to think only one of those entrypoints exists, causing compiler errors for users of the package.

  • Update exports entries to point at the correct files
  • Remove typings.d.ts from the files field, since it's now copied to the included lib folder

@atlassian-cla-bot
Copy link

atlassian-cla-bot bot commented Sep 19, 2023

Hooray! All contributors have signed the CLA.

@changeset-bot
Copy link

changeset-bot bot commented Sep 19, 2023

🦋 Changeset detected

Latest commit: 33690e2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
better-ajv-errors Patch

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

@duniul duniul changed the title chore: copy typings file to built folders Fix exports to work with TS with both ESM and CJS Sep 20, 2023
@duniul
Copy link
Author

duniul commented Dec 15, 2023

@torifat pinging in case you forgot about this PR! It should be safe to merge 👍

khakers added a commit to khakers/snmp2mqtt that referenced this pull request Jul 12, 2024
…for pnpm

Patch required for better-ajv-errors as they declare their types incorrectly. See atlassian/better-ajv-errors#176, fix based on atlassian/better-ajv-errors#177

Updates tsconfig
Bumps dependency versions
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.

Types are not correctly exported for a package supporting both ESM and CJS
1 participant