better-ajv-errors icon indicating copy to clipboard operation
better-ajv-errors copied to clipboard

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

Open duniul opened this issue 2 years ago • 6 comments

Context

The typings.d.ts are currently only exposed via the types field in package.json, which results in the following error in TypeScript:

error TS7016: Could not find a declaration file for module 'better-ajv-errors'. './node_modules/better-ajv-errors/lib/esm/index.mjs' implicitly has an 'any' type.
  There are types at '/Users/daniel/Projects/toca/toca-ts-packages/packages/confy/node_modules/better-ajv-errors/typings.d.ts', but this result could not be resolved when respecting package.json "exports". The 'better-ajv-errors' library may need to update its package.json or typings.

Potential fix

Follow the guidelines listed here: https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing

Basically, you would need to add a types field to each exports entry in package.json, both pointing to unique files.

Edit: PR with fix: https://github.com/atlassian/better-ajv-errors/pull/177

duniul avatar Sep 19 '23 09:09 duniul

Thanks for reporting this. I will try to fix it soon.

However, did you try using a different moduleResolution option? See https://publint.dev/[email protected]

torifat avatar Sep 19 '23 09:09 torifat

@torifat I did, but thought I'd report it anyway since that's not always a solution for everyone. Posted a PR with a fix that works for me locally 👍

duniul avatar Sep 19 '23 10:09 duniul

I will have a look. I was thinking about doing something similar to what I did in one of my other project for maximum compatibility.

torifat avatar Sep 19 '23 10:09 torifat

Yup, it's pretty much that! 👍

duniul avatar Sep 19 '23 10:09 duniul

@torifat When is the PR addressing this issue scheduled to be merged? Unfortunately in my project, I cannot use moduleResolution.

PyotSiili avatar Dec 04 '23 13:12 PyotSiili

@torifat Also wondering about getting this merged

outkine avatar Nov 14 '25 19:11 outkine