apollo-link icon indicating copy to clipboard operation
apollo-link copied to clipboard

TypeScript 3.6.2 removed GlobalFetch

Open tyler-johnson opened this issue 6 years ago • 10 comments

I am getting errors in apollo-link-http-common when trying to build my code now since GlobalFetch no longer exists.

node_modules/apollo-link-http-common/lib/index.d.ts:38:13 - error TS2304: Cannot find name 'GlobalFetch'.

38     fetch?: GlobalFetch['fetch'];

See the blog post on the update: https://devblogs.microsoft.com/typescript/announcing-typescript-3-6/#dom-updates

tyler-johnson avatar Aug 28 '19 20:08 tyler-johnson

My workaround is an additional file ./types/global-fetch.d.ts with "typeRoots": ["./types", "./node_modules/@types"] in tsconfig.json:

declare type GlobalFetch = WindowOrWorkerGlobalScope

This bug is fixed in 04d405a342903274ff67c42dae51a89ee20e72dc but there is still no new version in NPM repo.

dex4er avatar Aug 29 '19 09:08 dex4er

I am using apollo-client 2.6.2 in my Ionic application. It was working fine until I ran npm install again. I have started getting the following error in apollo-link:

[app-scripts] [12:45:13]  typescript: ......./node_modules/apollo-link-http/lib/httpLink.d.ts, line: 9
[app-scripts]             Cannot find name 'GlobalFetch'.
[app-scripts]        L8:  includeExtensions?: boolean;
[app-scripts]        L9:  fetch?: GlobalFetch['fetch'];
[app-scripts]       L10:  headers?: any;

I have been using TypeScript 3.3.333 but suddenly the build has started to fail with the 'GlobalFetch' issue. Can someone help me with a fix or workaround? It has become a showstopper for our application release.

The suggested workaround does not work as in my case.

satishmolletipipra avatar Sep 05 '19 07:09 satishmolletipipra

@satishmolletipipra My guess is you forgot to use "lib": ["dom"],

but you should be able to re-add this type as:

/// <reference lib="dom" />
declare interface GlobalFetch {
  fetch(input: RequestInfo, init?: RequestInit): Promise<Response>
}

to your local ./types/ directory.

dex4er avatar Sep 05 '19 08:09 dex4er

I am just using TypeScript 3.5 until this gets fixed (as well as some other unrelated issues). I just set this in my package.json: "typescript": "~3.5.2"

tyler-johnson avatar Sep 06 '19 00:09 tyler-johnson

I am just using TypeScript 3.5 until this gets fixed (as well as some other unrelated issues). I just set this in my package.json: "typescript": "~3.5.2"

This seems like the only way right now...

themaskedavenger avatar Sep 07 '19 11:09 themaskedavenger

@hwillson any chance to release this fix?

OneCyrus avatar Sep 08 '19 07:09 OneCyrus

just saw that this fix was released as 1.5.16. So i guess this issue is actually resolved.

OneCyrus avatar Sep 09 '19 12:09 OneCyrus

To clarify, that's 1.5.16 of apollo-link-http that now depends on the fixed apollo-link-http-common (which had the type error).

seansfkelley avatar Sep 30 '19 21:09 seansfkelley

Maybe this helps someone as I had problems with applying the workarounds. They didn't work for me probably because I use apollo-boost instead. Also tried yarn resolutions to force fetching of newer nested deps, but it did not help (maybe due to monorepo/nested package structure? didn't investigate fully).

A workaround that did work was to set skipLibCheck: true in tsconfig.json. I didn't like that though and ended up applying a patch with https://www.npmjs.com/package/patch-package:

diff --git a/node_modules/apollo-boost/lib/index.d.ts b/node_modules/apollo-boost/lib/index.d.ts
index 1f58c91..9234caa 100644
--- a/node_modules/apollo-boost/lib/index.d.ts
+++ b/node_modules/apollo-boost/lib/index.d.ts
@@ -22,7 +22,7 @@ export interface PresetConfig {
     uri?: string | UriFunction;
     credentials?: string;
     headers?: any;
-    fetch?: GlobalFetch['fetch'];
+    fetch?: WindowOrWorkerGlobalScope['fetch'];
     fetchOptions?: HttpLink.Options;
     clientState?: ClientStateConfig;
     onError?: ErrorLink.ErrorHandler;
diff --git a/node_modules/apollo-boost/src/index.ts b/node_modules/apollo-boost/src/index.ts
index c9f0751..648d1a8 100644
--- a/node_modules/apollo-boost/src/index.ts
+++ b/node_modules/apollo-boost/src/index.ts
@@ -31,7 +31,7 @@ export interface PresetConfig {
   uri?: string | UriFunction;
   credentials?: string;
   headers?: any;
-  fetch?: GlobalFetch['fetch'];
+  fetch?: WindowOrWorkerGlobalScope['fetch'];
   fetchOptions?: HttpLink.Options;
   clientState?: ClientStateConfig;
   onError?: ErrorLink.ErrorHandler;

rszalski avatar Dec 09 '19 15:12 rszalski

I am still seeing this issue. I can apply a patch as the ^ above mentions, but it makes me kind of nervous. Has this still not been fixed?

KristenLeach avatar May 18 '20 21:05 KristenLeach