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" field to package.json "exports" #78

Merged
merged 1 commit into from
Sep 2, 2022
Merged

Add "types" field to package.json "exports" #78

merged 1 commit into from
Sep 2, 2022

Conversation

futurGH
Copy link
Contributor

@futurGH futurGH commented Aug 26, 2022

In TypeScript 4.7, the nodenext and node16 module resolution options were added to better support native ESM in Node.js. When the package.json "exports" field exists, however, they require a "types" field to exist within each entrypoint, or else types won't be resolved when using one of these module resolution options.

Currently, using this library with either of those settings throws the following TypeScript error:

error TS7016: Could not find a declaration file for module '@splinetool/react-spline'. '/project-path/node_modules/@splinetool/react-spline/dist/react-spline.cjs.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/splinetool__react-spline` if it exists or add a new declaration (.d.ts) file containing `declare module '@splinetool/react-spline';

This PR fixes this issue by adding the "types" field to "exports" for the "./" entrypoint. The same change is required for @splinetool/runtime, but I couldn't find a link to a repository where I could submit the change.

Allows support for users to have "moduleResolution": "nodenext" or "node16" in their tsconfig from TypeScript 4.7 onwards.
@marcofugaro
Copy link
Contributor

marcofugaro commented Sep 2, 2022

Hey @futurGH, could you share your setup? I've just tried using react-spline with Typescript 4.8.2 via create-react-app and it worked no problem. Repro.

Shouldn't it fallback to the root "types" field in package.json?

@futurGH
Copy link
Contributor Author

futurGH commented Sep 2, 2022

Thanks for looking into this @marcofugaro. You can find a better explanation of this issue than I'm capable of at microsoft/TypeScript#46334. Essentially, the top-level "types" field only applies when importing from "main". When you compile to native ESM and use "nodenext" or "node16" for TS' module resolution, it instead defaults to the "." import map, which requires its own "types" field, as TS won't look up the top-level "types" field if it's not looking at the "main" field as the entrypoint.

@marcofugaro
Copy link
Contributor

Right, thanks for linking to the conversation.

@marcofugaro marcofugaro merged commit 48794b6 into splinetool:main Sep 2, 2022
@futurGH
Copy link
Contributor Author

futurGH commented Sep 2, 2022

Thanks for merging this! I'd also appreciate if a similar change could be made to the @splinetool/runtime package; I don't believe the source for the package is public for me to submit a PR to?

@marcofugaro
Copy link
Contributor

@futurGH a new version of @splinetool/runtime with this fix will be published early next week

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