Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add type information to exports.* in package.json #295

Merged
merged 1 commit into from
Sep 9, 2023

Conversation

dotboris
Copy link
Contributor

This PR fixes an issue where typescript project using node16 or nodenext module resolution fail to find types.

This results in the following error:

$ tsc
index.ts:1:37 - error TS7016: Could not find a declaration file for module 'streaming-iterables'. '/Users/bbera/code/oss/streaming-iterables/dist/index.mjs' implicitly has an 'any' type.
  There are types at '/Users/bbera/code/oss/streaming-iterables/__scratch/test-project/node_modules/streaming-iterables/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'streaming-iterables' library may need to update its package.json or typings.

1 import * as streamingIterables from 'streaming-iterables'
                                      ~~~~~~~~~~~~~~~~~~~~~


Found 1 error in index.ts:1

This change is based on the following documentation: https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing

Steps to reproduce

Create a test project with the following files:

packages.json:

{
  "name": "test-project",
  "type": "module",
  "devDependencies": {
    "typescript": "^5.1.6"
  },
  "dependencies": {
    "streaming-iterables": "^7.1.0"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es2016",
    "module": "Node16",
    "moduleResolution": "Node16",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": false
  }
}

index.ts:

import * as streamingIterables from 'streaming-iterables'
console.log(streamingIterables)

Then, run the following commands:

npm install
./node_modules/.bin/tsc

@reconbot
Copy link
Owner

reconbot commented Aug 9, 2023

I'm willing to try this out, however we may need to ship duplicate types?

It’s important to note that the CommonJS entrypoint and the ES module entrypoint each needs its own declaration file, even if the contents are the same between them. Every declaration file is interpreted either as a CommonJS module or as an ES module, based on its file extension and the "type" field of the package.json, and this detected module kind must match the module kind that Node will detect for the corresponding JavaScript file for type checking to be correct. Attempting to use a single .d.ts file to type both an ES module entrypoint and a CommonJS entrypoint will cause TypeScript to think only one of those entrypoints exists, causing compiler errors for users of the package.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants