rxjs6分区方式: Argument of type 'UnaryFunction' is not assignable to parameter of type 'OperatorFunction'

标签 rxjs6

当使用 Rxjs 6 时,IDE 在使用 partition 方法时报告错误 - 即使函数工作正常。

例如:我有这个示例代码:

import { of } from 'rxjs';
import { filter, partition } from 'rxjs/operators';

let obs = of(1,2,3,4,6,9,111,13,10,12);

let [a, b] = obs.pipe(
  filter(v => v > 3),
  partition(p => p % 2 == 0)
);

a.subscribe(v => {
  console.log(v);
});

b.subscribe(v => {
  console.log(v);
});

该代码运行良好,可以将输入分隔为奇数和偶数。但是,VSCode 和 StackBlitz 在调用 partition 方法的那一行报错:

Argument of type 'UnaryFunction, [Observable, Observable]>' is not assignable to parameter of type 'OperatorFunction'. Type '[Observable, Observable]' is not assignable to type 'Observable'. Property '_isScalar' is missing in type '[Observable, Observable]'.

这是 stackblitz 网址: https://stackblitz.com/edit/rxjs6-learn

最佳答案

基于 @cartant 发表的评论, 以及链接的 Github issue ,调用partition的正确语法如下:

const source = from([1, 2, 3, 4, 5, 6])
const [evens, odds] = partition((val: any) => val % 2 === 0)(source)

这消除了 IDE 中显示的错误

关于rxjs6分区方式: Argument of type 'UnaryFunction' is not assignable to parameter of type 'OperatorFunction' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51471190/

相关文章:

angular - RxJS 阻止 switchMap 首先发送请求

javascript - 在 Angular 中使用 shareReplay(1) - 仍然调用 http 请求?

testing - RxJs 大理石测试 : Assertion fail log hard to understand

server-side-rendering - ERROR 错误 : Uncaught (in promise): TypeError: i. BehaviorSubject 不是 Angular 10 SSR 中的构造函数

angular - 如何在 Angular 6 中使用 rxjs fromEvent 定位 HTML 元素

javascript - 将 API 请求从使用 Promise 迁移到 Observable(使用 axios)

angular - RxJS 'Map' 运算符在 Angular 6 中不起作用

angular - 如何使用 RxJS 在 Angular 6 中发出一系列 http 请求

javascript - 使用 rjxs 而不是 setTimeout 函数订阅的更优雅的方式?