typescript - Casting results from Observable.forkJoin to their respective types in Angular 2 -


let have component in angular 2 needs load 2 different things server before page displayed. i'd of things fire off , call 1 event handler when come telling page isloaded = true. let's have service class looks this.

export class myservice {    getstronglytypeddata1(): observable<strongdata1[]>{       return this.http.get('http://...').map((response:response) => <strongdata1[]>response.json());    }    getstronglytypeddata2(): observable<strongdata2[]>{          return this.http.get('http://...').map((response:response) => <strongdata2[]>response.json());    } } 

then have component uses service class this.

export class mycomponent implements oninit {    isloaded = false;    stronglytypeddata1: strongdata1[];    stronglytypeddata2: strongdata2[];     constructor(private myservice:myservice){ }     ngoninit(){       var requests [           this.myservice.getstronglytypeddata1(),          this.myservice.getstronglytypeddata2()       ];       observable.forkjoin(requests).subscribe(          results => {             this.stronglytypeddata1 = results[0];             this.stronglytypeddata2 = results[1];             this.isloaded = true;          });    } } 

the typescript compiler complaining cant convert type object type strongdata1[]. if change strongdata1 , strongdata2 "any", works fine. i'd rather not though because i'm losing benefit of typescript's strong typings.

how cast results forkjoin respective types?

for me works when add requests directly observable.forkjoin , use es6 destructing result array.

so code this

observable     .forkjoin(this.myservice.getstronglytypeddata1(), this.myservice.getstronglytypeddata2())     .subscribe(         ([typedata1, typedata2]) => {             this.stronglytypeddata1 = typedata1;             this.stronglytypeddata2 = typedata2;             this.isloaded = true;         }     ); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -