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

Number of TypeScript type errors #1617

Open
tyronedougherty opened this issue Aug 2, 2022 · 17 comments
Open

Number of TypeScript type errors #1617

tyronedougherty opened this issue Aug 2, 2022 · 17 comments
Labels

Comments

@tyronedougherty
Copy link

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

@JaviZamudio
Copy link

Hey! for what i have seen, this is mostly problem of the "fetch-blob" package. I dont know if it's because "node-fetch" is using deprecated packages, or because it's still an issue in the dependendcies. But the quick fix may be to ignore those errors or files in the compiler options.

@KamasamaK
Copy link

These errors actually started with version 3.2.9.

@kristoffer-zliide
Copy link

I think it was caused by bcfb71c, since https://github.com/jimmywarting/FormData/blob/master/esm.min.d.ts doesn't export the type properly, and tsc doesn't know that it should also generate types here: https://github.com/node-fetch/fetch-blob/blob/main/package.json#L20.

@kael-shipman
Copy link

I'm also experiencing this.... Any pathway to a fix?

@kristoffer-zliide
Copy link

@yo1dog: would you consider reverting your commit?

@yo1dog
Copy link
Contributor

yo1dog commented Aug 18, 2022

I have no control over the repo. I made my PR nearly a year ago (Sep 2021). Back then there was no issue. I believe 81b1378 from Jan 2022 is what is conflicting. I guess no one tested it again a year later before merging it in?

If node-fetch aims to be pollyfill for node, it needs to provide a polyfill for the types as well.

BTW, fetch is coming to node soon anyways: https://nodejs.org/docs/latest-v18.x/api/globals.html#fetch

@Nakroma
Copy link

Nakroma commented Aug 24, 2022

Reverting to 3.2.8 works as a workaround

@kristoffer-zliide
Copy link

But 3.2.8 has CVE-2022-2596. @jimmywarting: would you revert the merge of bcfb71c?

@alive2
Copy link

alive2 commented Aug 30, 2022

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

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

@IamFlowZ
Copy link

IamFlowZ commented Jan 23, 2023

Adding in some noise to say I've also ran into this issue. Not only when using node-fetch, but also affects isomorphic-unfetch. going to revert to 3.2.8 as a solve since it's a one-liner.

@Gobbees
Copy link

Gobbees commented Feb 17, 2023

Also experiencing this. Had to downgrade to 3.2.8 as well. Is there any plan to fix?

@bendersej
Copy link

bendersej commented Mar 14, 2023

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

@isobel-taylor
Copy link

I'm still getting this in 3.3.2.

@docwilco
Copy link

Same.

@docwilco
Copy link

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

This fails with

src/@types/global.d.ts:8:13 - error TS2749: 'Blob' refers to a value, but is being used as a type here. Did you mean 'typeof Blob'?

8   var Blob: Blob;
              ~~~~

for me

@docwilco
Copy link

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

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

This did work for me with 3.3.2

4ydx added a commit to autifyhq/autify-cli that referenced this issue Oct 23, 2023
node-fetch causing issues when upgrading
node-fetch/node-fetch#1617
@aryamohanan
Copy link

aryamohanan commented May 22, 2024

Is there someone currently addressing this issue? I've also encountered it, and it seems to have been persistent for some time. I'm looking for a permanent fix for this issue.

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

No branches or pull requests