After declaring a typed variable to hold data fetched from a service, I encountered an issue where the returned data did not match the specified type of the variable. Surprisingly, the variable still accepted the mismatched data.
My code snippet is as follows:
Data: Array<{Currency: string, Rate: number}>;
getData(): void {
this.DataService.getAll().subscribe(
(res: Array<any>) => {
this.Data = res;
},
(err) => {
console.log(err);
}
);
}
I attempted the following solution, which unfortunately didn't resolve the problem:
res: Array<{Currency: string, Rate: number}>
Update
This section showcases my service implementation:
getAll(): Observable<Array<{Currency: string, Rate: number}>> {
return this.http.get(`${this.baseUrl}/data`).pipe(
map((res) => {
this.data = res['data'];
return this.data;
}),
catchError(this.handleError));
}
UPDATE 2
I came across a similar issue documented here: https://github.com/angular/angular/issues/20770