Chevrotain / chevrotain

Parser Building Toolkit for JavaScript
https://chevrotain.io
Apache License 2.0
2.44k stars 199 forks source link

TypeScript gives an error when importing using "moduleResolution": "bundler" #1967

Closed baodart closed 11 months ago

baodart commented 11 months ago

Hi,

I see the following Typescript error when importing chevrotain in ESM:

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

Here's a minimal reproduction repo: https://github.com/baodart/chevrotain-typescript-issue

When googling the error, I found this tool https://arethetypeswrong.github.io (from https://github.com/microsoft/TypeScript/issues/52363) that could show more details:

image

In my local, if I go to node_modules/chevrotain/package.json and change the exports section to:

  "exports": {
    ".": {
      "import": "./lib/src/api.js",
      "types": "./chevrotain.d.ts"
    }
  },

Then my local shows no more error (not sure if it is the right fix 😂).

msujew commented 11 months ago

Funnily enough, I encountered this issue 5 minutes ago as well. I'll create a fix for that.

baodart commented 11 months ago

Thanks for the fix.