I am currently working on getting a full response from my HTTP calls. Following the guidelines in the Angular documentation, I have set my HTTP call options as {observe: 'response'}
However, when I implement this, I encounter the following error:
The type 'Observable<HttpResponse<IItem[]>>' is not compatible with the type 'Observable<IItem[]>'. The property 'includes' is missing in the 'HttpResponse<IItem[]>' type.
It seems my understanding of Observables and Typing/Casting is not completely clear.
In my HTTP Get request, I am receiving an observable which I am trying to strongly type into an array of items (as shown below)
getItems(): Observable <IItem[]>{
return this._http.get<IItem[]>(url, {observe: 'response'})
.do(data => console.log('All: ' + JSON.stringify(data)))
};
I attempted setting
getUsers(): Observable <HttpResponse<IUser[]>>
, which resolved the initial error. However, I encountered another error within my subscribe function when trying to assign the returned data to a local variable this.item = data;
, where item
is declared as Item[]
The type 'Observable<HttpResponse<IItem[]>>' is not assignable to type 'IItem[]
Therefore, my questions are as follows:
- How can I resolve the above error and what are the reasons behind it?
- Is it necessary to set the return type for the
method ifgetItems(): Observable <IItem[]>
._http.get<IItem[]>
already specifies the expected type of response?
It appears that there may be some fundamental concept that I am misunderstanding leading to this confusion. Any assistance would be greatly appreciated. As I am new to Angular 4, please provide guidance with patience.