Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use rxjs6 partition in typescript?

When I try to use rxjs6 partition in typescript a type missmatch error is thrown. Also the example from the official documentation throws that error. I think I have to cast something, but I don't know what. Any suggestions?

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)); 

Error:

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

See https://stackblitz.com/edit/typescript-fdqhws

like image 242
fastr.de Avatar asked Jun 14 '18 12:06

fastr.de


1 Answers

The typings for pipe are currently not flexible enough to support partition as a pipeable operator, and the documentation for partition isn't very helpful at the time of writing: https://github.com/ReactiveX/rxjs/issues/2995

I believe that there are calls for it to be made a static function in RxJS7: https://github.com/ReactiveX/rxjs/issues/3797

As a workaround, try this:

const clicks = fromEvent(document, 'click');
const parts = partition(ev => ev.target.tagName === 'DIV')(clicks);
like image 176
Wayne Maurer Avatar answered Nov 09 '22 10:11

Wayne Maurer