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

edit-package-json incompatibility with typescript 4.7.3, moduleResolution Node16 #47

Closed
paulzacz opened this issue Jun 24, 2022 · 10 comments
Assignees

Comments

@paulzacz
Copy link

Package's name
Which package from the monorepo is it for?

edit-package-json

Describe the bug
A clear and concise description of what the bug is.

Typescript cannot find the types for edit-package-json when using "moduleResolution": "Node16", on typescript v4.7.3

To Reproduce
Steps to reproduce the behavior:

tsconfig.json

{
  "compilerOptions": {
    "module": "esnext",
    "moduleResolution": "Node16",
  }
}

Sample file:

import { del, set } from 'edit-package-json';

Error message:

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

import { del, set } from 'edit-package-json';

Expected behavior
Typescript is able to find the type definitions.

Additional context
Typescript 4.7.3 added support for ESM modules in node.js, including the "exports" keyword in package.json. The relevant portion of package.json for edit-package-json is as follows:

  "exports": {
    "script": "./dist/edit-package-json.umd.js",
    "default": "./dist/edit-package-json.esm.js"
  },
  "types": "types/index.d.ts",

As written, Typescript 4.7.3 is looking for ./dist/edit-package-json.esm.d.ts, but that files does not exist. Either add a "types" within the "exports" section pointing to the right file, or change the build process to output a definition file in the expected location with the expected name. Typescript 4.7.3 exports reference

@revelt
Copy link
Member

revelt commented Jun 24, 2022

Thank you for reporting that and sorry for the bug. I'll patch it this weekend.

@revelt
Copy link
Member

revelt commented Jun 27, 2022

Interesting, it's happening on other libs too: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#comment-328

@revelt
Copy link
Member

revelt commented Jun 27, 2022

I recreated the setup with the aforementioned compilerOptions and TS 4.7.4:

TS transpiles fine using tsc! It's not enough to just put those compilerOptions and TS 4.7.4.

Please choose and fork one of the above and adapt to your breaking code, trimming it down until you are able to distill the minimal proof case.

Also, with regards to the type definitions being in the different folder — redux uses exactly the same path style "types": "types/index.d.ts",. That's a common pattern.

I'll close this issue now but when you have an MVP proof, re-open.

@revelt revelt closed this as completed Jun 27, 2022
@paulzacz
Copy link
Author

I reproduced the issue here: https://github.com/paulzacz/edit-package-json-issue-47-repro

Two significant edits:

  1. tsconfig "strict": true
  2. I don't think tsconfig was actually being read by tsc src/index.ts. VSCode reported the issue with your unmodified repo. I changed the build command to tsc --build instead.

@paulzacz
Copy link
Author

paulzacz commented Jun 27, 2022

Also, with regards to the type definitions being in the different folder — redux uses exactly the same path style "types": "types/index.d.ts",. That's a common pattern.

Yes, that's a common pattern, but a deprecated pattern. The major feature of Typescript 4.7.3 was first-class support for Node16 modules. Previously, Typescript didn't know about package.json exports, now it does so long as you have the correct tsconfig settings to enable it. Once it starts paying attention to "exports", it ignores the top-level "main" and "types".

@revelt revelt reopened this Jun 27, 2022
@revelt
Copy link
Member

revelt commented Jun 27, 2022

Thank you! Reopening.

@revelt
Copy link
Member

revelt commented Jul 4, 2022

linking TS ticket that discusses this microsoft/TypeScript#49160

@revelt revelt closed this as completed in 26a5470 Jul 5, 2022
@revelt
Copy link
Member

revelt commented Jul 5, 2022

Fixed in v0.6.5 patch, I checked the reproduction repo, types load fine. Closing. Thank you for all the help!

@revelt revelt closed this as completed Jul 5, 2022
@paulzacz
Copy link
Author

paulzacz commented Jul 5, 2022

Thank you as well. I had no idea you auto-generated your own package.json files so extensively. Also, I didn't know that JavaScript preserved the order of its object keys internally. I just assumed they were unordered hash maps under the hood.

@paulzacz
Copy link
Author

paulzacz commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants