deepkit-framework icon indicating copy to clipboard operation
deepkit-framework copied to clipboard

typescript-eslint error caused by `types` field in package.json

Open NexZhu opened this issue 2 years ago • 1 comments

When trying to use ESM ("type": "module") in my project, typescript-eslint is unable to find types, possibly caused by "types": "./dist/cjs/index.d.ts" in Deepkit packages' package.json:

{
  "name": "@deepkit/event",
  "version": "1.0.1-alpha.74",
  "description": "Deepkit async event dispatcher",
  "type": "commonjs",
  "main": "./dist/cjs/index.js",
  "module": "./dist/esm/index.js",
  "types": "./dist/cjs/index.d.ts",
  "exports": {
    ".": {
      "require": "./dist/cjs/index.js",
      "default": "./dist/esm/index.js"
    }
  }
}

image

NexZhu avatar Jul 20 '22 07:07 NexZhu

The latest Node specs call for ignoring other package.json fields when the exports field is present, so this issue can pop up based on the tsconfig module and module resolution settings, and depending on the Typescript version.

The fix is to add the types field to the export:

  "exports": {
    ".": {
      "types": "./dist/cjs/index.d.ts",
      "require": "./dist/cjs/index.js",
      "default": "./dist/esm/index.js"
    }

I've run into this a lot due to using beta and rc Typescript versions plus esm, and I typically use patch-package to address it.

adam-coster avatar Aug 20 '22 00:08 adam-coster

Thanks for the report @NexZhu and thanks for the fix @adam-coster, fixed in https://github.com/deepkit/deepkit-framework/commit/b04dae7f5f92820faf443aa3b1045f1a77b65d53

marcj avatar Dec 03 '22 11:12 marcj