lquixada / cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.
MIT License
1.67k stars 104 forks source link

Types error in index.d.ts for nodejs #35

Closed KyloJorgensen closed 5 years ago

KyloJorgensen commented 5 years ago

image

usesing vscode to write nodejs server code

lquixada commented 5 years ago

I'm not familiar with Typescript but I think fetch, Request, Response and Headers are supposed to be global types, aren't they?

In my VSCode, there's no such warning.

lquixada commented 5 years ago

Closing this since there was no answer.

esetnik commented 5 years ago

@lquixada please reopen. I have the same error as @KyloJorgensen. I don't think those are global types. I don't see them defined in the node type definitions at least.

/builds/x/api/node_modules/cross-fetch/index.d.ts (1,27): Cannot find name 'fetch'.
/builds/x/api/node_modules/cross-fetch/index.d.ts (2,27): Cannot find name 'Request'.
/builds/x/api/node_modules/cross-fetch/index.d.ts (3,27): Cannot find name 'Response'.
/builds/x/api/node_modules/cross-fetch/index.d.ts (4,31): Cannot find name 'Headers'. Did you mean 'headers'?
lquixada commented 5 years ago

hey @esetnik, can you try going to your tsconfig.json > compilerOptions > libs and add "dom" to the list?

will-wow commented 5 years ago

I was seeing the same issue - import fetch from "cross-fetch"; wasn't picking up any types for fetch, and the index.d.ts file showed Cannot find name 'fetch'.

Adding dom to libs fixed the issue for me. Thanks @lquixada!

esetnik commented 5 years ago

I don't think dom should be added to libs for node environments. This adds a lot of types that are only available in the browser.

lquixada commented 5 years ago

@esetnik you're right! however, if you follow the the fetch, Request, Response and Header declaration inside the lib.dom.d.ts, you'll realize that those are very deeply nested declarations. Unfortunately, there's no easy way to extract and maintain them. It would be needed a lot of copy and paste and it would be hard to keep track of any update.

In my opinion, having a single source of truth (the lib.dom.d.ts in this case) is more valuable than saving a few kilobytes that users would not have to download anyway.