Skip to content

add 'types' to package.json 'exports' root#370

Merged
modesty merged 1 commit intomodesty:masterfrom
jeremybanka:feat/node-module-types
Nov 3, 2024
Merged

add 'types' to package.json 'exports' root#370
modesty merged 1 commit intomodesty:masterfrom
jeremybanka:feat/node-module-types

Conversation

@jeremybanka
Copy link
Copy Markdown
Contributor

@jeremybanka jeremybanka commented Oct 14, 2024

Overview

When resolving modules with TypeScript via the package.json "exports" spec, the type declaration for this package is currently not resolved properly.

This PR fixes that by including the "types" field under "exports" > ".", and resolving to the existing type declaration file.

Steps to Reproduce

mkdir demo
cd demo

# Install Dependencies
npm i pdf2json@3.1.2
npm i typescript@5.6.3

# Configure TypeScript
# - We'll use a modern module resolution strategy, "Bundler", which leverages package.json exports.
# - ES2015 is the minimum target for the Bundler strategy.
echo '{"include":["main.ts"],"compilerOptions":{"target":"ES2015","moduleResolution":"Bundler"}}' >> tsconfig.json

# Create a Failing Example
echo 'import PDFParser from "pdf2json"' >> main.ts

# Run TypeScript
# "--skipLibCheck" ignores any internal issues from our dependencies.
# "--noImplicitAny" will error anything any-typed (no typesafety) in our code.
npx tsc --skipLibCheck --noImplicitAny

TypeScript will report,

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

1 import PDFParser from "pdf2json"
                        ~~~~~~~~~~


Found 1 error in main.ts:1

Background Information

@modesty modesty merged commit 9880a29 into modesty:master Nov 3, 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

Successfully merging this pull request may close these issues.

2 participants