RxJS在Typescript中扫描,具有不同类型的累加器和更新值



尝试使用不同类型的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
}

但是打字稿立即开始抱怨累加器不适用于定义的类型,它仅适用于设置modification: any,这就像根本不写打字稿一样。

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

不知道

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

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

最新更新