Sometimes I see that some codes handle an HTTP response like this:
f(): Observable<Hero[]> {
return this.http.get('local.host').map((response: Response) => response.json().data as Hero[]);
}
and Hero
export class Hero {
id: number;
name: string;
}
what
r.json().data as Hero[]
mean?
But as far as I know, now there is no direct function for converting a json object to a ts object, is the "as" keyword used to convert a json object to a custom ts object? if yes can i write
var myClass=r.json() as MyClass;
if(!myClass){
}
to check if json object can be converted to my custom class?
source
share