如何在打字稿中使用 rxjs6 分区?
How to use rxjs6 partition in 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]'.
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);
当我尝试在打字稿中使用 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]'.
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);