error TS2339: Property 'partition' does not exist on type 'Observable<boolean>'

Viewed 621

Previously I was using rxjs-5 and I was using observable.partition as follows:

const [isTiming$, isNotTiming$] = this.store.select(state => state.tetris.isTiming)
        .partition(value => value);

After upgrade angular to 8 rxjs got upgraded to rxjs-6 which started throwing following error:

 providers/timer.provider.ts(27,5): error TS2339: Property 'partition' does not exist on type 'Observable<boolean>'.

when I checked in older rxjs implementation it was implemented as follows:

  import { Observable } from '../Observable';
  import { partition as higherOrder } from '../operators/partition';
  /**
   * Splits the source Observable into two, one with values that satisfy a
   * predicate, and another with values that don't satisfy the predicate.
   */
   export function partition<T>(this: Observable<T>, predicate: (value: T, index: number) => boolean, thisArg?: any): [Observable<T>, Observable<T>] {
    return higherOrder(predicate, thisArg)(this);
  }
2 Answers

After seeing github conversion

I think we should deprecate the partition operator and remove it for v7.

Reasons:

  • Not really an operator: partition isn't really an "operator" in that it returns [Observable, Observable] rather than Observable. This means it doesn't compose via pipe like the others.

  • Easy to replace with filter: partition is easily replaced with the much more widely known filter operator. As partition is effectively the same thing as: const partition = (predicate) => [source.pipe(filter(predicate)), source.pipe(filter((x, i) => !predicate(x, i)))]

in your case:

import {filter} = "rxjs/operators"
const source = this.store.select(state => state.tetris.isTiming);
const partition = (predicate) => [source.pipe(filter(predicate)), source.pipe(filter((x, i) => !predicate(x, i)))]

const [isTiming$, isNotTiming$] = partition(value => value);
  • Rarely used: It's little used, by any code survey I've taken (within thousands of lines of code that I know use RxJS)

I guest you should use Observable method pipe, something like this :

const [isTiming$, isNotTiming$] = this.store.select(state => state.tetris.isTiming)
        .pipe(
            partition(value => value);
        )
Related