gpt4 book ai didi

typescript - RxJS 将 Observable 转换为 Observable

转载 作者:搜寻专家 更新时间:2023-10-30 21:06:41 25 4
gpt4 key购买 nike

RxJS 5.5.6。我正在尝试转换 Observable<string[]>进入Observable<string>

我不确定 mergeAll运算符是我要找的

const o1: Observable<string[]> = of(['a', 'b', 'c']);
const o2: Observable<string> = o1.pipe(
mergeAll()
);

Typescript 将返回此错误:

Type 'Observable<string | string[]>' is not assignable to type 'Observable<string>'.
Type 'string | string[]' is not assignable to type 'string'.
Type 'string[]' is not assignable to type 'string'.

我接收 Observable 作为参数,我可以改变构造方式。

最佳答案

您似乎遇到了一个已报告的 RxJS 问题,请参阅 https://github.com/ReactiveX/rxjs/issues/2759甚至最近https://github.com/ReactiveX/rxjs/issues/3290 .

从评论来看,它似乎要到 RxJS 6 才能修复。

但是,您始终可以使用 mergeMap(o => o)/concatMap(o => o) 而不是 mergeAll()/concatAll().

例如:

const o1: Observable<string[]> = of(['a', 'b', 'c']);
const o2: Observable<string> = o1.pipe(
mergeMap(o => o)
);

关于typescript - RxJS 将 Observable<string[]> 转换为 Observable<string>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48601357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com