【发布时间】:2019-08-29 08:15:32
【问题描述】:
尝试使用不同类型的 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 立即开始抱怨累加器不适用于定义的类型,并且它只适用于设置 modification: any,这就像根本不写 typescript。
我对 Rx 有误解吗?当我进入运行时时,这种模式以前对我有用。
【问题讨论】:
-
您可以使用
scan<T, R>(...)强制类型 -
它可能在抱怨
startWith( [] )。这样,您将在流中同时拥有[]和Request,您将使用scan进行处理。要告诉扫描在累加器中以[]开始,请使用scan(this.accumulator, [])
标签: typescript rxjs