I'm working with a Typescript Angular2 site that uses RxJS Observables. I am taking a piece of user data and hitting two different API endpoints. I'm .forkJoin
ing them together like so
var First: TypeA = this.api.FirstEndpoint(...);
var Second: TypeB = this.api.SecondEndpoint(...);
Observable.forkJoin(First, Second).subscribe((results: (TypeA|TypeB)[]) => {
var first = <TypeA>results[0];
var second = <TypeB>results[1];
...
});
While this technically works, I feel this is a bit clunky. Especially if I join more and more requests or a variable number of requests. Nothing says that results[0]
is TypeA or that 1
is TypeB.
Is there a smoother way to type results
or an alternative to forkJoin
that will combine requests but keep their responses different (and thus typed more explicitly)?