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

Add "types" import condition to fix esm type definition file not found #257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

isudzumi
Copy link

Summary

close #248

As of Typescript 4.7, when esmodule applications with moduleResolution: nodenext use this library, Typescript cannot find a declaration file for .mjs file (https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#package-json-exports-imports-and-self-referencing).
To fix the problem, add "types" field to import condition.

Details

About the problem

Here is example:

package.json

{
  "type": "module",
  "dependencies": {
    "urlcat": "^3.1.0"
  },
  "devDependencies": {
    "typescript": "^5.0.4"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "nodenext",
    "moduleResolution": "nodenext",
    "strict": true
  }
}

This emits the following error

index.ts:1:20 - error TS7016: Could not find a declaration file for module 'urlcat'. 'node_modules/urlcat/dist/index.mjs' implicitly has an 'any' type.
  There are types at 'node_modules/urlcat/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'urlcat' library may need to update its package.json or typings.

1 import urlcat from "urlcat";
                     ~~~~~~~~


Found 1 error in index.ts:1

This PR fixes the issue.

About postbuild

To find type definition file for .mjs, it requires .d.mts.
Ideally, tsup should add support for generating .d.mts or .d.cts file, but currently the feature is not implemented (egoist/tsup#760).
So this PR copies .d.ts to .d.mts in a postbuild script as a workaround.

@omarkhatibco
Copy link

could you update this PR, tsup supports exporting .d.cts now.
egoist/tsup#934

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 this pull request may close these issues.

Type definition cannot be resolved under moduleResolution: "NodeNext"
2 participants