got: Got 12.1.0 x Typescript 4.7.2 x Node 16.15 transpilation fails with moduleResolution=Node16

Describe the bug

  • Node.js version: 16.15.0
  • OS & version: Windows 11

Actual behavior

Requiring got and transpiling with Typescript 4.7 raises transpilation errors

node_modules/got/dist/source/core/options.d.ts(763,22): error TS2709: Cannot use namespace 'CacheableLookup' as a type.
node_modules/got/dist/source/core/options.d.ts(764,26): error TS2709: Cannot use namespace 'CacheableLookup' as a type.
node_modules/got/dist/source/core/options.d.ts(775,21): error TS2709: Cannot use namespace 'CacheableLookup' as a type.
node_modules/got/dist/source/core/options.d.ts(776,25): error TS2709: Cannot use namespace 'CacheableLookup' as a type.
node_modules/got/dist/source/core/options.d.ts(1125,29): error TS2709: Cannot use namespace 'CacheableLookup' as a type.

Code to reproduce

package.json

{
  "name": "got-ts4.7-regression",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "got": "^12.1.0"
  },
  "devDependencies": {
    "typescript": "^4.7.2"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "Node16",
    "moduleResolution": "Node16",
    "target": "es6",
  },
  "exclude": [
    "node_modules"
  ]
}

“moduleResolution”: “Node16” is apparently required for Typescript 4.7 support of the exports field in package.json

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 12
  • Comments: 22 (9 by maintainers)

Commits related to this issue

Most upvoted comments

I would go with 2. In the future, more packages are switching to node16, so if you choose 1, you’re just going to hit this problem with another package.

Alternatively, you could maybe set "skipLibCheck": true in your tsconfig.

I really feel like this issue should have a much higher priority. Almost all sindresorhus packages have been promoting ESM only for a long time, and now that TS finally have proper support, this issue alone prevents me from finally migrating a big TypeScript project to ESM.

Really hope someone picks this up soon, i will happily donate a cup of coffee to anyone moving on with this issue 😄 ❤️

I appreciate the push forward, but mainline releases really should be stable. The ESM requirement should have remained on a pre-release line until dependency issues were resolved. Yes, 11 is a fallback that most people use but new features all target 12.

Same, a little bit confused here. I cannot get this package to compile with TypeScript after the switch to ESM. Setting skipLibCheck to true in compilerOptions is the only way to get this package to compile. Using TypeScript 4.7.3 and got 12.1.0.

I feel like I’m missing something, does anyone have a barebones repo with TypeScript working?

node_modules/got/dist/source/core/options.d.ts:14:35 - error TS7016: Could not find a declaration file for module 'form-data-encoder'. '/home/akowald/projects/shipping-library/node_modules/form-data-encoder/lib/esm/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/form-data-encoder` if it exists or add a new declaration (.d.ts) file containing `declare module 'form-data-encoder';`

14 import type { FormDataLike } from 'form-data-encoder';
                                     ~~~~~~~~~~~~~~~~~~~

node_modules/got/dist/source/core/options.d.ts:763:22 - error TS2709: Cannot use namespace 'CacheableLookup' as a type.

763     get dnsLookup(): CacheableLookup['lookup'] | undefined;
                         ~~~~~~~~~~~~~~~

node_modules/got/dist/source/core/options.d.ts:764:26 - error TS2709: Cannot use namespace 'CacheableLookup' as a type.

764     set dnsLookup(value: CacheableLookup['lookup'] | undefined);
                             ~~~~~~~~~~~~~~~

node_modules/got/dist/source/core/options.d.ts:775:21 - error TS2709: Cannot use namespace 'CacheableLookup' as a type.

775     get dnsCache(): CacheableLookup | boolean | undefined;
                        ~~~~~~~~~~~~~~~

node_modules/got/dist/source/core/options.d.ts:776:25 - error TS2709: Cannot use namespace 'CacheableLookup' as a type.

776     set dnsCache(value: CacheableLookup | boolean | undefined);
                            ~~~~~~~~~~~~~~~

node_modules/got/dist/source/core/options.d.ts:1125:29 - error TS2709: Cannot use namespace 'CacheableLookup' as a type.

1125         dnsCache: boolean | CacheableLookup | undefined;
                                 ~~~~~~~~~~~~~~~


Found 6 errors in the same file, starting at: node_modules/got/dist/source/core/options.d.ts:14

I’ve released then-busboy@5.2.1 and form-data-encoder@2.1.2 with these fixes. Thanks @Baune8D!

I temporarily solved this issue by removing /// <reference types="node" resolution-mode="require"/> from node_modules/cacheable-request/dist/types.d.ts and running npx patch-package cacheable-request, see Maxim-Mazurok/google-api-typings-generator@c1f1654 (#703)

Now that https://github.com/sindresorhus/got/issues/2129 is closed, this is another issue that blocks got from being used in TypeScript with ESM because cacheable-request now requires node16 to be set due to:

node_modules/cacheable-request/dist/types.d.ts(1,23): error TS1452: 'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.
node_modules/cacheable-request/dist/types.d.ts(2,23): error TS1452: 'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.
node_modules/cacheable-request/dist/types.d.ts(3,23): error TS1452: 'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.
node_modules/cacheable-request/dist/types.d.ts(4,23): error TS1452: 'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.

Frankly, last few releases from @sindresorhus (not only Got) been a sick joke introducing more problems to people that chose to follow his advises and use ESM.

We hear you @Baune8D ❤️ Unfortunately there’s just only me and @sindresorhus. I’m quite busy with full-time work, I’ll try to find time later this week to push this forward, but no promises. We encourage more people to make contributions ❤️

To fix this, we need to upgrade AVA to v4 and then upgrade @sindresorhus/tsconfig to v3. Contributions welcome.