Multicast Operator with Pipe in RxJS 5.5+

心不动则不痛 提交于 2020-01-04 05:54:49

问题


How do I use the multicast() operator with the new recommended approach in RxJS 5.5 of using pipe() instead of chaining operators? I get a TypeScript error when I try to use connect() like I did before:

const even$ = new Subject();

const connectedSub = interval(500)
    .pipe(
        filter(count => count % 2 === 0),
        take(5),
        multicast(even$)
    )
    .connect();

even$.subscribe(value => console.log(value));

This code works but yields a TypeScript error that reports that Property 'connect' does not exist on type 'Observable<{}>'. Am I using connectable observables the way that I should be in RxJS 5.5+?


回答1:


The current - v5.5.10 and v6.1.0 - typings for pipe are not aware of the Observable subclasses, so I use a type assertion, like this:

const connectedObs = interval(500).pipe(
    filter(count => count % 2 === 0),
    take(5),
    multicast(even$)
) as ConnectableObservable<number>;
const connectedSub = connectedObs.connect();


来源:https://stackoverflow.com/questions/50166366/multicast-operator-with-pipe-in-rxjs-5-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!