错误 TS2339:“Observable<boolean>"类型上不存在属性“partition" [英] error TS2339: Property 'partition' does not exist on type 'Observable<boolean>'

查看:45
本文介绍了错误 TS2339:“Observable<boolean>"类型上不存在属性“partition"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前我使用 rxjs-5,我使用 observable.partition 如下:

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

angular 升级到 8rxjs 升级到 rxjs-6 开始抛出以下错误:

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>'.

当我检查旧的 rxjs 实现时,它的实现如下:

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

推荐答案

看到github转换后

我认为我们应该弃用分区运算符并在 v7 中将其删除.

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

原因:

  • 并不是真正的运算符:partition 并不是真正的运算符",因为它返回 [Observable, Observable] 而不是 Observable.这意味着它不像其他的那样通过管道组合.

  • 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.

易于用过滤器替换:分区很容易被更广为人知的过滤器操作符替换.由于分区实际上与以下内容相同: const partition = (predicate) =>;[source.pipe(filter(predicate)), source.pipe(filter((x, i) => !predicate(x, i)))]

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

在你的情况下:

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

  • 很少使用:根据我所做的任何代码调查,它很少使用(在我知道使用 RxJS 的数千行代码中)
  • 这篇关于错误 TS2339:“Observable&lt;boolean&gt;"类型上不存在属性“partition"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆