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

'FormData' refers to a value, but is being used as a type here #1657

Open
IlyaSavichItr opened this issue Sep 30, 2022 · 7 comments
Open

'FormData' refers to a value, but is being used as a type here #1657

IlyaSavichItr opened this issue Sep 30, 2022 · 7 comments
Labels

Comments

@IlyaSavichItr
Copy link

IlyaSavichItr commented Sep 30, 2022

Reproduction

Hi, I've got some issues with node-fetch v3.2.10 while trying to upgrade from v2.6.7 (tbf these issues appeared starting from v3.0.0).

When trying to compile typescript getting

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;
            ~~~~~~~~

Or

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>;
                         ~~~~~~~~

Expected behavior

I expect not to get any issue

Your Environment

software version
node-fetch 3.2.10
node 18.1.0
yarn 3.2.0
Operating System WSL2.0 Ubuntu 20.04, Windows 10

Tried typescript v.4.7.4 and v4.8.4, both gets the same result

@ritschwumm
Copy link

ritschwumm commented Oct 15, 2022

i'm not quite sure why this doesn't work any more. i'd really like to know, so i'd be glad if someone could help me find out.

to me it seems as if the real culprit is formdata-polyfill which does not polyfill any of the types for FormData except the declaration that it exists and can be constructed - but then, how could it possibly have worked before?

i noticed that adding the "dom" lib to tsconfig.json makes the compile error go away. as a workaround this might be good enough, but i'd very much prefer not pretending my node app has a full browser dom available...

@qtiki
Copy link

qtiki commented Oct 20, 2022

i noticed that adding the "dom" lib to tsconfig.json makes the compile error go away. as a workaround this might be good enough, but i'd very much prefer not pretending my node app has a full browser dom available...

Trying to use @types/node and @types/web at the same time often leads to conflicts in the types. I tried with a clean project and just adding node-fetch with both types I get a compiler error from AbortSignal type mismatch:

node_modules/@types/web/index.d.ts:2048:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'AbortSignal' must be of type '{ new (): AbortSignal; prototype: AbortSignal; }', but here has type '{ new (): AbortSignal; prototype: AbortSignal; timeout(milliseconds: number): AbortSignal; }'.

2048 declare var AbortSignal: {
                 ~~~~~~~~~~~

  node_modules/@types/node/ts4.8/globals.d.ts:72:13
    72 declare var AbortSignal: {
                   ~~~~~~~~~~~
    'AbortSignal' was also declared here.


Found 1 error in node_modules/@types/web/index.d.ts:2048

@ritschwumm
Copy link

well.. with node 18 you'd probably not need node-fetch anyway. if they had types... DefinitelyTyped/DefinitelyTyped#60924

@qtiki
Copy link

qtiki commented Oct 24, 2022

well.. with node 18 you'd probably not need node-fetch anyway

Yes, I am eagerly awaiting for the AWS Lambda runtime support for Node 18. In the meantime I'm thinking of reverting to an older node-fetch and using that until the runtime with native fetch is supported.

@DarkMatterMatt
Copy link

This issue is likely to be a duplicate of #1617, introduced in #1287. Workarounds are to revert to v3.2.8 or add DOM to the libraries included in tsconfig.

@SufyaanKhateeb
Copy link

This issue is likely to be a duplicate of #1617, introduced in #1287. Workarounds are to revert to v3.2.8 or add DOM to the libraries included in tsconfig.

Can you explain why adding the 'DOM' to tsconfig resolves the problem?

@DarkMatterMatt
Copy link

Hi @SufyaanKhateeb

Can you explain why adding the 'DOM' to tsconfig resolves the problem?

The 'DOM' library includes type definitions for FormData, which you can see here: https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts#L6029

Usually we want to avoid using DOM (browser) type definitions in Node, because it's a completely separate environment. In this case it should be relatively safe because node-fetch's first feature in the README is that it "stays consistent with window.fetch API".

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

5 participants