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

情到浓时终转凉″ 提交于 2019-12-07 17:04:21

问题


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);
  }

回答1:


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)



回答2:


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);
        )


来源:https://stackoverflow.com/questions/56609155/error-ts2339-property-partition-does-not-exist-on-type-observableboolean

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