node-fetch: Number of TypeScript type errors

Reproduction

Steps to reproduce the behavior:

  1. yarn install node-fetch (3.2.10)
  2. tsc -p tsconfig.json --noEmit
  3. get the following errors:
$ tsc -p tsconfig.json --noEmit
node_modules/fetch-blob/file.d.ts:1:76 - error TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

1 /** @type {typeof globalThis.File} */ export const File: typeof globalThis.File;
                                                                             ~~~~

node_modules/fetch-blob/from.d.ts:20:64 - error TS2749: 'File' refers to a value, but is being used as a type here. Did you mean 'typeof File'?

20 export function fileFrom(path: string, type?: string): Promise<File>;
                                                                  ~~~~

node_modules/fetch-blob/from.d.ts:25:60 - error TS2749: 'File' refers to a value, but is being used as a type here. Did you mean 'typeof File'?

25 export function fileFromSync(path: string, type?: string): File;
                                                              ~~~~

node_modules/fetch-blob/index.d.ts:2:38 - error TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

2 export const Blob: typeof globalThis.Blob;
                                       ~~~~

node_modules/formdata-polyfill/esm.min.d.ts:2:11 - error TS2749: 'FormData' refers to a value, but is being used as a type here. Did you mean 'typeof FormData'?

2   new (): FormData;
            ~~~~~~~~

node_modules/formdata-polyfill/esm.min.d.ts:3:14 - error TS2749: 'FormData' refers to a value, but is being used as a type here. Did you mean 'typeof FormData'?

3   prototype: FormData;
               ~~~~~~~~

node_modules/formdata-polyfill/esm.min.d.ts:5:50 - error TS2749: 'FormData' refers to a value, but is being used as a type here. Did you mean 'typeof FormData'?

5 export declare function formDataToBlob(formData: FormData): Blob;
                                                   ~~~~~~~~

node_modules/node-fetch/@types/index.d.ts:124:4 - error TS2749: 'FormData' refers to a value, but is being used as a type here. Did you mean 'typeof FormData'?

124  | FormData
       ~~~~~~~~

node_modules/node-fetch/@types/index.d.ts:137:22 - error TS2749: 'FormData' refers to a value, but is being used as a type here. Did you mean 'typeof FormData'?

137  formData(): Promise<FormData>;
                         ~~~~~~~~


Found 9 errors in 5 files.

Errors  Files
     1  node_modules/fetch-blob/file.d.ts:1
     2  node_modules/fetch-blob/from.d.ts:20
     1  node_modules/fetch-blob/index.d.ts:2
     3  node_modules/formdata-polyfill/esm.min.d.ts:2
     2  node_modules/node-fetch/@types/index.d.ts:124
error Command failed with exit code 2.

My tsconfig.json looks like this:

{

  "compileOnSave": true,
  "compilerOptions": {
    "declaration": true,
    "esModuleInterop": true,
    "lib": ["ES2021"],
    "module": "CommonJS",
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "outDir": "dist",
    "resolveJsonModule": true,
    "rootDir": "src",
    "strict": true,
    "target": "ES2021",
    "typeRoots": ["./src/@types", "./node_modules/@types"]
  },
  "exclude": ["**/__tests__", "node_modules"],
  "include": ["./src/**/*.ts"]
}

Expected behavior

There should be no type errors

Screenshots

Your Environment

software version
node-fetch 3.2.10
node 16.16.0
yarn 1.22.19
typescript 4.7.4
Operating System MacOS 12.4

Additional context

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 21
  • Comments: 16 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Adding “DOM” to tsconfig.json also works as a temporary workaround

{
    "compilerOptions": {
        ...
        "lib": ["es2017", "esnext.asynciterable", "DOM"],
        ...
    }
}

Reverting to 3.2.8 works as a workaround

These errors actually started with version 3.2.9.

A better approach than adding DOM, is to define the following:

./src/@types/global.d.ts

export {};

declare global {
  type FormData = import('formdata-node').FormData;
  type File = import('formdata-node').File;

  var File: File;
  var Blob: Blob;
}

./tsconfig.json

{
  "compilerOptions": {
    // other options
    "lib": ["ES2022"]
  },
}

Note that formdata-node is necessary for this to work