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

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

问题描述

以前,我使用的是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升级到8 rxjs升级到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.

原因:

  • 不是真正的运算符:分区并不是真正的运算符",因为它返回[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.

易于使用filter替换:分区很容易用更广为人知的filter运算符替换.由于分区实际上与以下内容相同: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;"上不存在属性“分区"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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