My Api service contains a simple function called getUsers
, which is used to fetch all the users on the api.
public getUsers(url: string): Observable<IUser[]> {
return this._http.get(url);
}
The IUser interface I have created has all fields marked as optional for now.
export interface IUser {
id?: string;
first_name?: string;
last_name?: string;
location?: string;
followers?: string;
following?: string;
checkins?: string;
image?: string;
}
Here is how I have implemented the service in my component:
export class SocialOverviewContainerComponent implements OnInit {
public userData = [];
public showForm = {};
private _apiService: ApiService;
constructor(apiService: ApiService) {
this._apiService = apiService
}
public ngOnInit(): void {
this.getUsersData();
}
public getUsersData() {
this._apiService.getUsers(ApiSettings.apiBasepath + 'users/')
.subscribe(users => {
this.userData = users;
})
}
}
I encountered a Type error when compiling:
ERROR in src/app/services/api.service.ts(18,5): error TS2322: Type 'Observable<Object>' is not assignable to type 'Observable<IUser[]>'.
Type 'Object' is not assignable to type 'IUser[]'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Property 'includes' is missing in type 'Object'.
I believe my response matches the interface, so I am confused about the error. I have made the field optional for now, so I'm unsure why this issue is occurring.
Although I could resolve it by casting the observable as any, that would defeat the purpose of using Typescript. Any insights on where I might be going wrong would be greatly appreciated.
Thank you in advance