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

TS and node16 modules #891

Closed
danielepolencic opened this issue Jul 5, 2022 · 5 comments
Closed

TS and node16 modules #891

danielepolencic opened this issue Jul 5, 2022 · 5 comments

Comments

@danielepolencic
Copy link

danielepolencic commented Jul 5, 2022

Codemirror does not compile with typescript 4.7 when "module": "node16" and "moduleResolution": "node16" in the tsconfig. Error:

some-path/src/code-mirror.tsx(1,40): error TS7016: Could not find a declaration file for module 'codemirror'. '/private/var/tmp/_bazel/198d81e4f6aafdc263b492f2c04a5df7/sandbox/darwin-sandbox/959/execroot/project/node_modules/codemirror/dist/index.cjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/codemirror` if it exists or add a new declaration (.d.ts) file containing `declare module 'codemirror';`

Why

In a few words, TS introduced new ways to publish and consume type definitions and TS files depending on if you code using ESM or Commonjs.

What I tried to fix it

According to this, adding an index.d.cts does the trick. So I copy-pasted the index.d.ts and renamed it. Then I removed types from the package.json and it did not work.

Also

The same issue seems to affect other packages as well:

  • @codemirror/view
  • @codemirror/state
  • @codemirror/lang-markdown

Context

  • Node 16.2.0
  • codemirror 6.0.1
  • @codemirror/lang-markdown 6.0.0
  • Typescript 4.7.2
@marijnh
Copy link
Member

marijnh commented Jul 5, 2022

I expect this will break with a lot of npm packages. Weird decision of the TypeScript team to define that option in such a way that it doesn't work with the way most packages are being published (single declaration file using ES exports). Does anybody know about a non-invasive way around this (that doesn't break with the old resolution setting)?

@revelt
Copy link

revelt commented Sep 6, 2022

I had similar troubles with types not recognised on "strict" mode on TS 4.7, similar to here, and in the end we found that types should also go inside exports, as the first entry. Adapting to @codemirror/view, the relevant keys would be:

// package.json
{
  "name": "@codemirror/view",
  "type": "module",
  "exports": {
    "types": "./dist/index.d.ts", // <-- add this, notice the dot-slash, also the position is important
    "import": "./dist/index.js",
    "require": "./dist/index.cjs"
  },
  "types": "dist/index.d.ts" // <-- still keep, notice no dot in front
}

I updated all my open source npm packages that way.

@jodyheavener
Copy link

Here's a bit more context as well: microsoft/TypeScript#49160 (comment)

I'm eager to see this fixed, so I would be happy to take a stab at it.

@prichey
Copy link

prichey commented Jan 24, 2023

I have an open PR for this in codemirror/state: codemirror/state#3. If it gets traction I'm happy to introduce similar requests across all the other repos as well. I believe this approach is sufficiently non-invasive (as it is backwards / forwards compatible with both Node and TS)

@marijnh
Copy link
Member

marijnh commented Aug 30, 2023

I think this should be fixed since June now.

@marijnh marijnh closed this as completed Aug 30, 2023
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 a pull request may close this issue.

5 participants