Skip to content

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

Open
isudzumi wants to merge 2 commits intobalazsbotond:masterfrom
isudzumi:add-types-import-condition
Open

Add "types" import condition to fix esm type definition file not found#257
isudzumi wants to merge 2 commits intobalazsbotond:masterfrom
isudzumi:add-types-import-condition

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.

@omarkhatibgg
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