Typescript 整洁的RXJS观测值

Typescript 整洁的RXJS观测值,typescript,rxjs,observable,rxjs6,Typescript,Rxjs,Observable,Rxjs6,我使用的是RXJS 6.5.4 在我的typescript服务中,我有: isAuthenticated(): Observable<boolean> { // request } currentUserPromise(): Promise<User> { // request the User model and return via Promise. } isAuthenticated():可观察{ //请求 } currentUserPromise()

我使用的是RXJS 6.5.4

在我的typescript服务中,我有:

isAuthenticated(): Observable<boolean> {
   // request
}
currentUserPromise(): Promise<User> {
   // request the User model and return via Promise.
}
isAuthenticated():可观察{
//请求
}
currentUserPromise():Promise{
//请求用户模型并通过Promise返回。
}
我的第二种方法是,我需要验证会话是否经过身份验证,然后请求当前用户并将其转换为另一个可观察对象,以确定当前用户是否是管理员

我正在尝试这个,但我得到了错误:

isAdmin(): Observable<boolean> {   
   this.isAuthenticated().pipe(map(authenticated => {
      if (authenticated) {   
          return from(this.currentUserPromise().then(user => user.roles.indexOf('ADMIN') > -1)
      }
      return mapTo(false); // there is no authentication, so no current user and no admin!
   }));
}
isAdmin():可观察的{
this.isAuthenticated().pipe(映射(已验证=>{
如果(已验证){
返回自(this.currentUserPromise()。然后(user=>user.roles.indexOf('ADMIN')>-1)
}
返回mapTo(false);//没有身份验证,因此没有当前用户和管理员!
}));
}
这是编译错误:

Type 'Observable<Observable<boolean>>' is not assignable to type 'Observable<boolean>'.
   Type 'Observable<boolean>' is not assignable to type 'boolean'.ts(2322)
类型“Observable”不可分配给类型“Observable”。
类型“Observable”不可分配给类型“boolean”。ts(2322)

如果你需要订阅一个内部可观察的对象,你需要一个更高阶的操作符,比如
switchMap
map
用于同步数据转换。
mapTo
是一个操作符,不能像使用它那样使用它,使用
将一个值转换成可观察的对象…你还需要返回它以使其成为可观察的对象订阅给

isAdmin(): Observable<boolean> {   
   return this.isAuthenticated().pipe(switchMap(authenticated => {
      if (authenticated) {   
          return from(this.currentUserPromise().then(user => user.roles.indexOf('ADMIN') > -1))
      }
      return of(false); // there is no authentication, so no current user and no admin!
   }));
}
isAdmin():可观察的{
返回此.isAuthenticated().pipe(switchMap(authenticated=>{
如果(已验证){
从(this.currentUserPromise().then(user=>user.roles.indexOf('ADMIN')>-1))返回
}
返回(false);//没有身份验证,因此没有当前用户和管理员!
}));
}