错误 TS2339:“Observable<boolean>"类型上不存在属性“partition" [英] error TS2339: Property 'partition' does not exist on type '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);
}
推荐答案
我认为我们应该弃用分区运算符并在 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<boolean>"类型上不存在属性“partition"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!