0

I am trying to map data from two observables to a third one like

  return this.coursesService
  .findCourseByUrl(route.params['id'])
  .pipe(
    switchMap((course: Course) =>
      this.coursesService
        .findLessonsForCourse(course.id)
        .pipe(map((lessons: Lesson[])=> [course, lessons)])
    )
  );

But I am getting the following exception

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[])[]'.

I found out that the resultSelector in switchMap is deprecated in rxJs6, that's why I was trying this approach. But got stuck in here.

Vijender Kumar
  • 1,285
  • 2
  • 17
  • 26

1 Answers1

0

Figured out following two ways, not sure about the second solution though.

The first solution: Added types explicitly while mapping the final observable.

return this.coursesService
  .findCourseByUrl(route.params['id'])
  .pipe(
    switchMap((course: Course) =>
      this.coursesService
        .findLessonsForCourse(course.id)
        .pipe(map(lessons => [course, lessons] as [Course, Lesson[]])),
    ),
  );

Second solution

return this.coursesService
  .findCourseByUrl(route.params['id'])
  .pipe(
    switchMap((course: Course) =>
      this.coursesService
        .findLessonsForCourse(course.id)
        .pipe(merge(lessons => [course, lessons])),
    ),
  );
Vijender Kumar
  • 1,285
  • 2
  • 17
  • 26