lezer-parser / highlight

Syntax highlighting from Lezer trees
MIT License
31 stars 9 forks source link

Could not find a declaration file for module '@lezer/highlight' #3

Closed diegohaz closed 1 year ago

diegohaz commented 1 year ago

This is a TypeScript error when I try to use @lezer/highlight in an ESM project using TypeScript:

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

marijnh commented 1 year ago

What is your moduleResolution option? nodenext/node16 can be weird like this.

diegohaz commented 1 year ago

Yes, I'm using nodenext/node16. That's the only way to work with the exports field in a TS project as far as I know.

marijnh commented 1 year ago

Is upgrading to TS 5.0 and using "moduleResolution": "bundler" an option for you?

The reason I'm pushing back on this is that I'd have to change the package.json and tag a new release for all my packages (which is a lot of them), and that I'm still confused on what the proper format even looks like (the PR you submitted goes against the recommendations in this page, for example). I'm rather hoping that developments on the TS side will make this annoying requirement go away.

diegohaz commented 1 year ago

I'm using TS 5.0, but, as I'm building an npm package, this paragraph about "moduleResolution": "bundler" scares me:

On the other hand, if you’re writing a library that’s meant to be published on npm, using the bundler option can hide compatibility issues that may arise for your users who aren’t using a bundler. So in these cases, using the node16 or nodenext resolution options is likely to be a better path.

From my understanding, the ideal solution would be to name the type definition files the same as the source files (index.d.ts and index.d.cts). This way, they don't even need to be mentioned in package.json.

marijnh commented 1 year ago

I'm using TS 5.0, but, as I'm building an npm package, this paragraph about "moduleResolution": "bundler" scares me:

I am hoping, given the issues with node16-style resolution, that "bundler" becomes widely used, and the community can avoid the extra cruft in every package.json.

From my understanding, the ideal solution would be to name the type definition files the same as the source files (index.d.ts and index.d.cts). This way, they don't even need to be mentioned in package.json.

That would explain why you only had the issue with this package, and not the others. Attached patch adjust the build process to emit a dist/index.d.ts instead. Does that address the problem for you?

diegohaz commented 1 year ago

Yes, I updated the file to index.d.ts directly in the node_modules, and the issue was fixed. 👍

marijnh commented 1 year ago

Great. I've tagged 1.1.4 with this change.