首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >类型'Observable<(T | R[])[]>‘不能赋值给类型'Observable<[T,R[]]>

类型'Observable<(T | R[])[]>‘不能赋值给类型'Observable<[T,R[]]>
EN

Stack Overflow用户
提问于 2018-08-25 18:18:56
回答 1查看 35关注 0票数 0

我正在尝试将两个观察点的数据映射到第三个观察点,如下所示

代码语言:javascript
运行
复制
  return this.coursesService
  .findCourseByUrl(route.params['id'])
  .pipe(
    switchMap((course: Course) =>
      this.coursesService
        .findLessonsForCourse(course.id)
        .pipe(map((lessons: Lesson[])=> [course, lessons)])
    )
  );

但是我得到了以下异常

代码语言:javascript
运行
复制
Type 'Observable<(Course | Lesson[])[]>' is not assignable to type 'Observable<[Course, Lesson[]]>'.
Type '(Course | Lesson[])[]' is not assignable to type '[Course, Lesson[]]'.
Property '0' is missing in type '(Course | Lesson[])[]'.

我发现switchMap中的resultSelector在rxJs6中被弃用,这就是我尝试这种方法的原因。但却被困在这里。

EN

回答 1

Stack Overflow用户

发布于 2018-08-25 18:54:04

想出了以下两种方法,但不确定第二种解决方案。

第一个解决方案:在映射最终的可观察对象时显式添加类型。

代码语言:javascript
运行
复制
return this.coursesService
  .findCourseByUrl(route.params['id'])
  .pipe(
    switchMap((course: Course) =>
      this.coursesService
        .findLessonsForCourse(course.id)
        .pipe(map(lessons => [course, lessons] as [Course, Lesson[]])),
    ),
  );

第二种解决方案

代码语言:javascript
运行
复制
return this.coursesService
  .findCourseByUrl(route.params['id'])
  .pipe(
    switchMap((course: Course) =>
      this.coursesService
        .findLessonsForCourse(course.id)
        .pipe(merge(lessons => [course, lessons])),
    ),
  );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52016268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档