当使用 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/