Hi I am learning Angular 8 and as a learner I have many doubts. I am sharing one of my doubts in detail. I hope some of you can easily help and correct me.
I have a service which consumes to Web API and returns some company details like
0: {CompanyId: 1, Name: "xxxx", Address: "bn"}
1: {CompanyId: 2, Name: "yyyy", Address: "tv"}
service.ts
GetAll(): Observable<IEmployee>{
return this.httpClient.get<IEmployee>(this.apiUrl + "GetCompany_test").}
component.ts
private emp : IEmployee;
getAllEmployees(){
this.service.GetAll().subscribe(
response => {this.emp =response;console.log(response)},
error => console.log(error)
);}
IEmployee.ts
export interface IEmployee{
fullName:string,
Email:string,
Mobile:string,
City:string,
HireDate:Date
}
I get the company details even though I use Observable of Observable<IEmployee>
. So what is the need of casting here? and when I cast to Employee and I easily get non Employee data,it should so me some warning or error in the console right? I am totally confused about what is happening.
Can somebody please help me to understand the concept of casting here and advice me to correctly use casting.
Regards.