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

TypeScript cannot find .d.ts when using moduleResolution: NodeNext #5

Open
NWilson opened this issue Jun 17, 2024 · 4 comments
Open

Comments

@NWilson
Copy link

NWilson commented Jun 17, 2024

I recently updated my tsconfig to use moduleResolution: NodeNext (which enables "exports" processing in package.json).

However, I am still producing CommonJS output using "module: NodeNext" in tsconfig with "type: commonjs".

This causes TypeScript to load the "require" branch in your exports, which has no typings:

"exports": {
    ".": {
      "require": "./lib/sha256-uint8array.js",
      "import": {
        "types": "./types/sha256-uint8array.d.ts",
        "default": "./dist/sha256-uint8array.mjs"
      }
    }
  },

You should export the typings for both require&import clients as follows:

"exports": {
    ".": {
      "types": "./types/sha256-uint8array.d.ts",
      "require": "./lib/sha256-uint8array.js",
      "import": "./dist/sha256-uint8array.mjs"
    }
  },
@kawanet
Copy link
Owner

kawanet commented Jun 17, 2024

@NWilson Thank you for the reporting. I've already almost forgotten the exact reasons but I guess the current complex tricky structrure of exports["."].import.types is made due to support another build system.

Hmm, found. Issue #3 made the change.

On your environment, the following plan B or C do work? (not tested on the environments though)

Plan B (types duplicated)

"exports": {
    ".": {
      "require": "./lib/sha256-uint8array.js",
      "types": "./types/sha256-uint8array.d.ts",
      "import": {
        "types": "./types/sha256-uint8array.d.ts",
        "default": "./dist/sha256-uint8array.mjs"
      }
    }
  },

Plan C (types duplicated and require/import separated)

"exports": {
    ".": {
      "require": {
        "types": "./types/sha256-uint8array.d.ts",
        "default": "./lib/sha256-uint8array.js"
      },
      "import": {
        "types": "./types/sha256-uint8array.d.ts",
        "default": "./dist/sha256-uint8array.mjs"
      }
    }
  },

@NWilson
Copy link
Author

NWilson commented Jun 17, 2024

Wow, thanks for a quick reply!

I have tested plan A (my one) and it works.

I have tested your plan B, and it doesn't work, because the options listed are always tried first-to-last, and so because you put "require" before "types", the compiler just picks the "require" option and doesn't see the types. (However, it would work if you put "types" very first.)

I have tested your plan C, and works. To me, it seems equally correct & tidy as plan A. I have no preference or suggestion between A and C.

Thank you!

@kawanet
Copy link
Owner

kawanet commented Jun 18, 2024

@NWilson great! Could you give me your tsconfig.json or something? I could not reproduce the issue.
I've tested it with https://github.com/kawanet/module-resolution-sandbox/blob/main/ts-esm/tsconfig.json

I recently updated my tsconfig to use moduleResolution: NodeNext (which enables "exports" processing in package.json).
However, I am still producing CommonJS output using "module: NodeNext" in tsconfig with "type: commonjs".

@NWilson
Copy link
Author

NWilson commented Jun 18, 2024

I have checked out your repo, and tested.

I have two changes compared to your ts-esm test:

  1. package.json has type: commonjs (yours has type: module, so TypeScript is loading the "import" field from sha256-uint8array, which is fine)
  2. tsconfig.json has strict: true (this is necessary to make TypeScript complain when it can't find the .d.ts)

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

2 participants