I have this piece of code that works just fine in Angular 5 but I'm trying to update to Angular 8:
Rxjs changed from version 5 to version 6 during your upgrade. This change had an impact on how types are handled by TypeScript, because version 6 does a better job at inferring types.
this.awsService.getProfiles().subscribe(....)
One of the big changes between Angular 5 towards Angular 6 was the switch from HttpModule
to the new HttpClientModule
, and this module introduced seraizlied JSON support.
For example;
function getProfiles() {
return this.http.get<MyInterfaceType>(....);
}
In the above, the GET request will deserialize the JSON object into the interface type MyInterfaceType
.
This feature, is not directly added to your source code when you completed the automated upgrade. So you likely have some older style code like this.
function getProfiles() {
return this.http.get(....);
}
This introduces a number of type challenges for TypeScript.
- there is no declared return type for the function and it has to be inferred
- the return type for
http.get()
is an Observable<Response>
type and not the JSON type
And I'm getting this error:
The fact that the error relates to an ambigious Object
type implies that you haven't updated the code for awsService()
to properly use the new HttpClientModule
, and you haven't defined a proper return type for getProfiles()
.
There are a couple of approaches here:
- define a return type for the
getProfiles(): Observable<any[]>
to silence the error, but this might not give you runnable code.
- update the HTTP to serialize to JSON objects by defining the type as
http.get<Profile[]>(...)
as an example
- define a parameter type for the
subscribe((profiles: any[]) => {...})
Either way, I don't think you're all the way there with your upgrade.
It's easier to try and get your unit tests working, then trying to get your entire application running. While you can silence some of these TypeScript errors. It's unclear from the question if it's a symptom of the code, an upgrade issue or just a mismatched type.