typescript - 如何在 typescript 中使用 rxjs6 分区?

标签 typescript rxjs rxjs6

当我尝试在 typescript 中使用 rxjs6 partition 时,会抛出类型不匹配错误。官方文档中的示例也会引发该错误。我想我必须施放一些东西,但我不知道是什么。有什么建议吗?

import {fromEvent} from 'rxjs';
import {partition} from 'rxjs/operators';

document.body.innerHTML = "<DIV width=100 height=100></DIV>"


//Example Code from https://rxjs-dev.firebaseapp.com/api/operators/partition
const clicks = fromEvent(document, 'click');
const parts = clicks.pipe(partition(ev => ev.target.tagName === 'DIV'));
const clicksOnDivs = parts[0];
const clicksElsewhere = parts[1];
clicksOnDivs.subscribe(x => console.log('DIV clicked: ', x));
clicksElsewhere.subscribe(x => console.log('Other clicked: ', x)); 

错误:

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

参见 https://stackblitz.com/edit/typescript-fdqhws

最佳答案

pipe 的类型目前不够灵活,无法支持 partition 作为可管道运算符,而且 partition 的文档也不是很清楚在撰写本文时有帮助: https://github.com/ReactiveX/rxjs/issues/2995

我相信有人要求在 RxJS7 中将其设为静态函数: https://github.com/ReactiveX/rxjs/issues/3797

作为解决方法,试试这个:

const clicks = fromEvent(document, 'click');
const parts = partition(ev => ev.target.tagName === 'DIV')(clicks);

关于typescript - 如何在 typescript 中使用 rxjs6 分区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50857298/

相关文章:

javascript - 无法在订阅主体内的 setState Hook 中调用先前状态的对象方法

javascript - 在 Angular 2 中订阅 FormControl 真实值变化的最佳方式?

angular - 如何使用 angulafire2 正确初始化和分配可观察值?

typescript - 是否可以为具有可变深度的嵌套对象定义类型?

typescript - 如何在子类中正确键入返回值( typescript )

javascript - 如何将图像传递给函数 Ionic 2

angular - angular 中的 observable、observer 和 subscribe 是什么?

rx-java - Scala.js Observables 查询

typescript - 通过泛型在 TypeScript 中将元组转换为对象

angular - 如何在不需要 rxjs-compat 的情况下只导入 RxJS 6 中使用的运算符,如旧的 RxJS?