Skip to content

Mixed types in pipes with mergeMap and more than 8 parameters yield typescript error #3766

@akehir

Description

@akehir

In the below code, foo1() is ok, but foo2() is not. In other words, in pipes with more than 8 parameters in the stream, the type definition assumes that all the observables of the stream return the same types.

RxJS version:
6.2.0

Code to reproduce:
Stackblitz: https://stackblitz.com/edit/typescript-e8dcm5?file=index.ts

import {
  of,
  Observable,
} from 'rxjs';

import {
  tap,
  mergeMap,
} from 'rxjs/operators';

export class Foo {

  public foo1(): Observable<boolean> {
    return of(true).pipe(
      tap(), // 1
      tap(), // 2
      tap(), // 3
      tap(), // 4
      tap(), // 5
      tap(), // 6
      tap(), // 7
      mergeMap(() => {
        return of('');
      }),
      mergeMap(() => {
        return of(true);
      }),
    );
  }

  public foo2(): Observable<boolean> {
    return of(true).pipe(
      tap(), // 1
      tap(), // 2
      tap(), // 3
      tap(), // 4
      tap(), // 5
      tap(), // 6
      tap(), // 7
      tap(), // 8
      mergeMap(() => {
        return of('');
      }),
      mergeMap(() => {
        return of(true);
      }),
    );
  }
}

Expected behavior:
No typescript compilation error, as mergeMap should be able to change the type of the Observable, and only the last mergeMap should be relevant for the resulting type.

Actual behavior:

ERROR in index.ts(41,7): error TS2345: Argument of type 'OperatorFunction<boolean, string>' is not assignable to parameter of type 'OperatorFunction<boolean, boolean>'.
  Type 'string' is not assignable to type 'boolean'.

Additional information:
The issue seems to be with Observable.ts, line 292, and mergeMap.ts, line 13:

pipe<R>(...operations: OperatorFunction<T, R>[]): Observable<R>;
export function mergeMap<T, R>(project: (value: T, index: number) => ObservableInput<R>, concurrent?: number): OperatorFunction<T, R>;

With more than 8 parameters, apparently all the OperatorFunction<T, R> need to return the same type .

Metadata

Metadata

Assignees

No one assigned

    Labels

    TSIssues and PRs related purely to TypeScript issuesbugConfirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions