Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observable.forkJoin wrong return type when more than 6 arguments

I have an issue with Observable.forkJoin inferring the wrong return type and then causing errors when I pass more than 6 arguments.

Observable.forkJoin(service.getType1, service.getType2, service.getType3 ...)
        .subscribe(x => {
            this.type1Arr = x[0];
            this.type2Arr = x[1];
            this.type3Arr = x[2];

Each function call from the service returns an Observable<Array<type>>. The compiler is determining that the return should be Type1[][] when I have more than 6 calls from the service passed in. It works fine up to 6 though, it will have the correct return and I can assign strongly typed results.

I'm using rxjs 5.4.3 and Typescript 2.4.0 (Typescript Tools for Visual Studio is 2.5.2).

Is there a workaround for this without casting it?

like image 301
Feanor Avatar asked Sep 21 '17 17:09

Feanor


People also ask

What happens if observables are not provided in forkjoin?

If no input observables are provided (e.g. an empty array is passed), then the resulting stream will complete immediately. forkJoin will wait for all passed observables to emit and complete and then it will emit an array or an object with last values from corresponding observables.

Is it possible to send more than 6 parameters to forkjoin?

Currently, based on the answer to another related question it doesn't appear possible to send more than 6 parameters to forkJoin. However, based on the offical documentation, it says "forkJoin is an operator that takes any number of Observables which can be passed either as an array or directly as arguments."

What happens when forkjoin emits an error?

Overall, in order for forkJoin to emit a value, all given observables have to emit something at least once and complete. If any given observable errors at some point, forkJoin will error as well and immediately unsubscribe from the other observables.

How does forkjoin work in Java?

In order for the resulting array to have the same length as the number of input observables, whenever any of the given observables completes without emitting any value, forkJoin will complete at that moment as well and it will not emit anything either, even if it already has some last values from other observables.


1 Answers

The typings for forkJoin define max forkJoin with 6 parameters as you can see here: https://github.com/ReactiveX/rxjs/blob/master/src/observable/ForkJoinObservable.ts#L27

Note that there are different ways to call forkJoin with more than 6 parameters:

Observable.forkJoin(observables)

or

Observable.forkJoin(...observables)

You can also force return types (https://github.com/ReactiveX/rxjs/blob/master/src/observable/ForkJoinObservable.ts#L35):

Observable.forkJoin<Whatever[]>(observables)
like image 98
martin Avatar answered Sep 29 '22 07:09

martin