首页 > 解决方案 > 在 Typescript 中使用不同类型的 RxJS 扫描累加器和更新值

问题描述

尝试使用不同类型的 RxJS 进行“输入和输出”:

a$: Subject<Request>;
b$: Observable<Pair[]>;

在我传递请求的地方,它在scan函数中得到处理,并添加到累加器中:

this.b$ = this.a$.pipe(
  startWith([]),
  scan(this.accumulator),
  shareReplay(),
);

accumulator(acc: Pair[], modification: Request): Pair[] {
  // .... process request and add the pair derived from request to acc
  return acc
}

但是 typescript 立即开始抱怨累加器不适用于定义的类型,并且它只适用于 setting modification: any,这就像根本不写 typescript 一样。

我对 Rx 有误解吗?当我进入运行时,这种模式以前对我有用。

标签: typescriptrxjs

解决方案


不知道您是否找到了答案,但您所要做的就是拆分主题和观察者定义:

https://stackblitz.com/edit/typescript-rxjs-scan


推荐阅读