【问题标题】:RxJS partition. ERROR - is missing the following properties from typeRxJS 分区。错误 - 类型中缺少以下属性
【发布时间】:2020-12-28 07:12:49
【问题描述】:

有代码:

    ngOnInit(): void {
        [this.newReleaseNotes$, this.readReleaseNotes$] = partition(this.getReleaseNotes$(), ({ id }: ReleaseNoteInterface) =>
            this.releaseNotesService.readReleaseNotes.includes(id),
        );
    }

    private getReleaseNotes$(): Observable<ReleaseNoteInterface[]> {
        return forkJoin(this.releaseNotes.map((releaseNoteId: string) => this.releaseNotesService.fetchReleaseNoteById$(releaseNoteId)));
    }

此代码在 TypeScript 中引发错误:

错误 TS2345:'({ id }: ReleaseNoteInterface) => boolean' 类型的参数不可分配给 '(value: ReleaseNoteInterface[], index :数字)=>布尔'。 [0] 参数“__0”和“值”的类型不兼容。 [0] 类型“ReleaseNoteInterface[]”缺少类型“ReleaseNoteInterface”的以下属性:id、type、importance、actuality 等 8 个。

方法 this.releaseNotesService.fetchReleaseNoteById $ 向服务器发出请求并返回Observable&lt;ReleaseNoteInterface&gt;

如何正确使用partition运算符来解决我的问题?

【问题讨论】:

  • 你能澄清你想要做什么吗?因为这似乎你在滥用分区

标签: angular rxjs rxjs-observables


【解决方案1】:

@bryan60,感谢您的解释!有点不同:

    ngOnInit(): void {
        this.newReleaseNotes$ = this.getReleaseNotes$(this.releaseNotes.filter((id: string) => !this.releaseNotesService.readReleaseNotes.includes(id)));
        this.readReleaseNotes$ = this.getReleaseNotes$(this.releaseNotes.filter((id: string) => this.releaseNotesService.readReleaseNotes.includes(id)));
    }

    private getReleaseNotes$(releaseNotesIds: string[]): Observable<ReleaseNoteInterface[]> {
        return forkJoin(releaseNotesIds.map((releaseNoteId: string) => this.releaseNotesService.fetchReleaseNoteById$(releaseNoteId)));
    }

最初,我只是想使用partion 函数完美地解决这个问题。 也许有一个选项可以将forkJoin 替换为partion 可以使用的其他东西?

【讨论】:

  • 您所做的似乎是一个很好的解决方案。分区不是您要在此处使用的运算符。
【解决方案2】:

forkJoin 发出其内部 observables 的数组,因此 partition 期待类型 ReleaseNoteInterface[],而不是类型 ReleaseNoteInterface。

所以您的 partition 必须在该数组上进行操作:

    partition(this.getReleaseNotes$(), (releaseNotes: ReleaseNoteInterface[]) =>
        // unclear what to do with the array here?
        releaseNotes.some(({id}) => this.releaseNotesService.readReleaseNotes.includes(id))
    );

这将创建两个可观察的流,一个是数组中的任何 id 满足条件,一个是数组中的任何 id 都不满足条件。这是否是您想要的令人怀疑。

partition 用于将一个 observable 流拆分为 2 个流。虽然forkJoin 是一个更高阶的可观察对象,用于将多个可观察对象组合为一个,而forkJoin 根据定义只发出一次,并且一旦所有内部都完成,它将发出一个包含所有内部可观察对象的数组。所以在forkJoin 上使用partition 是不合逻辑的,因为partition 的存在是为了拆分多个发射流,而forkJoin 只是一个可观察到的发射。

你可能想要做的是:

ngOnInit(): void {
   const sharedNotes$ = this.getReleaseNotes$(); // get the sahred

   this.newReleaseNotes$ = sharedNotes$.pipe( // map and filter the array
     map(releaseNotes => releaseNotes.filter(({id}) => this.releaseNotesService.readReleaseNotes.includes(id)))
   )

   this.readReleaseNotes$ = sharedNotes$.pipe( // map and filter the array
     map(releaseNotes => releaseNotes.filter(({id}) => !this.releaseNotesService.readReleaseNotes.includes(id)))
   )

}

private getReleaseNotes$(): Observable<ReleaseNoteInterface[]> {
    return forkJoin(this.releaseNotes.map((releaseNoteId: string) => this.releaseNotesService.fetchReleaseNoteById$(releaseNoteId))).pipe(
      share() // share it to avoid multiple calls
    );
}

这为您提供了两个 observables,两个版本说明数组,一个包含满足条件的项目,另一个包含不满足条件的项目。

还有其他合乎逻辑的方法来设置它,例如只有一个符合接口 { newReleaseNotes: ReleaseNoteInterface[], readReleaseNotes: ReleaseNoteInterface[] } 的 observable 并使用 map 运算符转换为该接口。

【讨论】:

  • 在单独的答案中添加了评论
猜你喜欢
  • 2020-05-23
  • 1970-01-01
  • 2021-01-05
  • 2020-04-07
  • 1970-01-01
  • 2019-09-11
  • 2023-02-07
  • 2017-02-15
  • 2021-04-20
相关资源
最近更新 更多