Type Promise Response is not assignable to type Promise class

Today my app was working till I did an update to my libraries and then I started getting this error in all my service classes:

Type Promise<Response> is not assignable to type Promise<class>
Type Response is not assignable to type class

Here is what I did to solve it:

First, here is a code snippet of what was causing the error:

myFunction(): Promise<myClass>{
  return this.http.fetch('resturl')
    .then(response => response.json());  //this is now triggering the error
}

Google wasn’t any help (maybe I’m the only one screwing this up?) so I dug into it some more.

My first solution is a bit of a hack b/c is doesn’t enforce type safety (but it got me out of the jam for the moment…)

myFunction(): Promise<myClass>{
  return this.http.fetch('restapi')
    .then(response => response.json() as any);
}

By casting the results of response.json() as any, no type safety is enforced and my code compiles.

I still wasn’t happy because I wanted to do it the right way. Using VS Code I right-clicked on response.json() and it took me to the definition:

declare class Body {
	bodyUsed: boolean;
	arrayBuffer(): Promise;
	blob(): Promise;
	formData(): Promise;
	json(): Promise;
	json<T>(): Promise<T>;
	text(): Promise<string>;
}

It turns out that the json() method allows you to pass a type identifier to it, so my new code is such:

myFunction(): Promise<myClass>{
  return this.http.fetch('resturl')
    .then(response => response.json<myClass>());
}

This is much clearer and enforces type safety for my http calls. Voila!