Observable.forkJoin wrong return type when more than 6 arguments

淺唱寂寞╮ 提交于 2019-12-10 17:47:03

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/46350396/observable-forkjoin-wrong-return-type-when-more-than-6-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!