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

Under module resolution NodeNext declaration file lookup does not use "types" property of package.json #51996

Closed
ottokruse opened this issue Dec 23, 2022 · 2 comments
Assignees
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ottokruse
Copy link

ottokruse commented Dec 23, 2022

Bug Report

Under module resolution NodeNext declaration files (*.d.ts) need to be right next to the corresponding js files. It does not work to have them elsewhere but point to the right location with the "types" property of package.json.

For example, aws-jwt-verify has the following installed directory structure, with both CJS and ESM files (using exports to select the right file at runtime), but 1 set of typings at the root:

.
└── node_modules/
    └── aws-jwt-verify/
        ├── dist/
        │   ├── esm/
        │   │   ├── index.js
        │   │   ├── package.json // type: "module"
        │   │   └── ...
        │   └── cjs/
        │       ├── index.js
        │       ├── package.json // type: "commonjs"
        │       └── ...
        ├── package.json // type: "commonjs", types: "index.d.ts", exports: { ... }
        ├── index.d.ts
        └── ...

importing aws-jwt-verify fails with:

index.ts:1:32 - error TS7016: Could not find a declaration file for module 'aws-jwt-verify'. '/private/tmp/nodenext-resolution-issue/node_modules/aws-jwt-verify/dist/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/aws-jwt-verify` if it exists or add a new declaration (.d.ts) file containing `declare module 'aws-jwt-verify';`

image

Setting module resolution to "node" fixes this.

(Our library does work under NodeJS and ESM, so it works with NodeJS module resolution. I.e. this a typings issue only.)

🔎 Search Terms

moduleresolution nodenext

🕗 Version & Regression Information

moduleResolution NodeNext was introduced in TypeScript 4.7

⏯ Playground Link

I can't reproduce this in the playground (a different error is thrown), but this would be it:

// @moduleResolution NodeNext

import { JwtRsaVerifier } from "aws-jwt-verify";

console.log(JwtRsaVerifier);

Workbench Repro

💻 Code

Repo with code that reproduces the issue:

https://github.com/ottokruse/nodenext-resolution-issue

🙁 Actual behavior

Module declaration can't be found.

🙂 Expected behavior

Module declaration can be found.

@ottokruse ottokruse changed the title With module resolution NodeNext an error is raised for library aws-jwt-verify Under module resolution NodeNext declaration file lookup does not use "types" property of package.json Jan 2, 2023
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jan 4, 2023
@weswigham weswigham added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Needs Investigation This issue needs a team member to investigate its status. labels Jan 5, 2023
@weswigham
Copy link
Member

weswigham commented Jan 5, 2023

exports has priority over types. If a package has types and an exports map, it should have types entries in its export map. It has to be this way or we can't respect export maps and their ability to block entrypoints from being accessible.

@ottokruse
Copy link
Author

Makes total sense, thanks for the help.

tmillican added a commit to tmillican/iti that referenced this issue Jan 9, 2023
Required to support `"moduleResolution": "NodeNext"`. See:
microsoft/TypeScript#51996
tmillican added a commit to tmillican/iti that referenced this issue Jan 10, 2023
Two issues:

1. The `exports` map in `package.json` needs to include the `types`
key. See: microsoft/TypeScript#51996

2. Explicit `.js` extension is needed for the `export ... from`
statements in `src/index.ts` to satisfy ESM.

Point 2 in turn required a minor tweak to the import statement in
two of the tests (must import `createContainer from `./src/iti`,
not merely `./src`).
tmillican added a commit to tmillican/iti that referenced this issue Jan 10, 2023
Two issues:

1. The `exports` map in `package.json` needs to include the `types`
key. See: microsoft/TypeScript#51996

2. Explicit `.js` extension is needed for the `export ... from`
statements in `src/index.ts` to satisfy ESM.

Point 2 in turn required a minor tweak to the import statement in
two of the tests (must import `createContainer from `./src/iti`,
not merely `./src`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants