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

BUG: Incorrect commonjs types #18

Closed
CloudNStoyan opened this issue Oct 21, 2024 · 7 comments
Closed

BUG: Incorrect commonjs types #18

CloudNStoyan opened this issue Oct 21, 2024 · 7 comments

Comments

@CloudNStoyan
Copy link

CloudNStoyan commented Oct 21, 2024

Reproduction repo:
https://github.com/CloudNStoyan/jsdoccomment-incorrect-types-demo

The project doesn't export the correct commonjs types and this generates the following error when used in a commonjs typescript project:
image

The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@es-joy/jsdoccomment")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts' or create a local package.json file with `{ "type": "module" }`.ts(1479)

Forking the repo and running npx @arethetypeswrong/cli --pack found this problem:
https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseESM.md

And running npx check-export-map found problems in the "exports" property in the package.json

When I fixed those problems by copying index.d.ts to index.d.cts and replace the "exports" property with the following exports:

"exports": {
    ".": {
      "import": {
        "types": "./dist/index.d.ts",
        "default": "./src/index.js"
      },
      "require": {
        "types": "./dist/index.d.cts",
        "default": "./dist/index.cjs.cjs"
      }
    },
    "./package.json": "./package.json"
  },

The error was gone, but I am not sure if the index.d.ts types are the correct ones for commonjs as I see they are generated using @typhonjs-build-test/esm-d-ts.

@brettz9
Copy link
Contributor

brettz9 commented Oct 21, 2024

@typhonrt : Could you take a look at this?

Btw, our CI is breaking; is there anything you can do to fix the @typhonjs-typedoc/typedoc-pkg dependency issues?

@typhonrt
Copy link
Contributor

typhonrt commented Oct 22, 2024

I'll take a look and get back to this issue on Thursday.

I'm pretty sure I have an example of an ESM / CJS package (gl-matrix) that I've recently worked on that also just uses ESM declarations and I was able to test the package with CJS and it worked in VSCode. It might actually be necessary to convert the types to CJS / .cts for the require exports as they are ESM TS declarations.

Re: @typhonjs-typedoc/typedoc-pkg there is a new version and the typedoc version needs to be raised as well. I'll take a look at that too.

@typhonrt
Copy link
Contributor

Hi @CloudNStoyan.

Admittedly, I do spend my time in the ESM or TS generating ESM world.

From my general research and I think this is in line with what you reported that the declaration files between .d.mts or .d.ts with "type": "module" in package.json are identical for .d.cts output. The only thing I found that would differ from that is archaic / very old / or the so called node10 resolution similar to types that DefinitelyTyped uses; even then I believe they have to update to the later standard.

When I fixed those problems by copying index.d.ts to index.d.cts and replace the "exports" property with the following exports:
....

This is the same thing I saw that at least with rudimentary testing including your jsdoccomment-incorrect-types-demo demo repo.

It's more of a Typescript / TSC complaining / requiring explicit .d.cts of the same declaration file. From a handful of repos that I found that happened to offer .d.cts / .d.mts files configured in package.json I saw the same declaration file used for both.

When I tested types for my gl-matrix PR which includes CJS / ESM exports I simply made a direct CJS project in VSCode and it had no problems pulling in the types referenced by a single .d.ts file, but I wasn't using Typescript generating a CJS project like your demo repo. So it seems TSC is just more sensitive to requiring explicit types or use of .d.cts even if it is directly a duplicate of .d.ts.


If this is the case and my general understanding is that it's just an explicit resolution aspect / "quirk even" for TS and that the declaration file can be duplicated and simply renamed .d.cts then I can easily add an emitCJS option to esm-d-ts that simply creates a duplicate declaration file with .d.cts extension. IE file copy / change extension.


For @brettz9. I do believe I pointed out that the jsdoccomment types reference eslint / estree / @typescript-eslint/types. My PR a while back had those listed as dependencies, but they were removed by you.

Without the @types/eslint / @types/estree, etc. that were listed in the dependencies of package.json when compiling with TSC you will get errors unless using skipLibCheck in local tsconfig.json. Not sure if you want to address that as well.

image

@brettz9
Copy link
Contributor

brettz9 commented Oct 24, 2024

Go ahead and add in the types packages as dependencies... I can prepare a PR of course, if that's all that will be necessary...

@typhonrt
Copy link
Contributor

I think it's reasonable to add the emitCTS option to esm-d-ts just so things are automated, get a new version out, update the typedoc-pkg support and such in a single PR.

@typhonrt
Copy link
Contributor

Got the PR and it has detailed notes on changes. For CI concerns Node 16.x was dropped and 22.x added. vitest and possibly other packages are now 18+. Should be all good now.

@brettz9
Copy link
Contributor

brettz9 commented Oct 25, 2024

Fixed in #19 . Released as v0.50.0.

@brettz9 brettz9 closed this as completed Oct 25, 2024
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

No branches or pull requests

3 participants