Observable.forkJoin 超过 6 个参数时错误的返回类型

Fea*_*nor 5 observable rxjs rxjs5 angular

我有一个 Observable.forkJoin 推断错误返回类型的问题,然后当我传递超过 6 个参数时导致错误。

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

来自服务的每个函数调用都会返回一个Observable<Array<type>>. 编译器确定返回值应该是Type1[][]当我从传入的服务中调用超过 6 次时。虽然它最多可以正常工作 6 次,但它会得到正确的返回值,并且我可以分配强类型的结果。

我正在使用 rxjs 5.4.3 和 Typescript 2.4.0(Visual Studio 的 Typescript 工具是 2.5.2)。

有没有办法解决这个问题而不铸造它?

mar*_*tin 5

对于分型forkJoin定义最多forkJoin6个参数,你可以在这里看到:https://github.com/ReactiveX/rxjs/blob/master/src/observable/ForkJoinObservable.ts#L27

请注意,forkJoin使用超过 6 个参数进行调用有多种不同的方法:

Observable.forkJoin(observables)
Run Code Online (Sandbox Code Playgroud)

或者

Observable.forkJoin(...observables)
Run Code Online (Sandbox Code Playgroud)

您还可以强制返回类型(https://github.com/ReactiveX/rxjs/blob/master/src/observable/ForkJoinObservable.ts#L35):

Observable.forkJoin<Whatever[]>(observables)
Run Code Online (Sandbox Code Playgroud)

  • 他们移动了 src 链接:https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/forkJoin.ts (2认同)