What does the "how" in "response.json () like Hero []" do?

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){
    //error
}

to check if json object can be converted to my custom class?

+4
source share
2 answers

As @HarryNinh said

TypeScript 1.6 .tsx. : JSX TypeScript, ( JSX TypeScript). :

var x = <any> foo;
// is equivalent to:
var x = foo as any;

: TypeScript 1.6 ChangeLog

+2

casting

r.json().data as Hero[]

. , List of Object . - .

. json

+3

Source: https://habr.com/ru/post/1660253/


All Articles