Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return false instead of undefined

On an Angular 11 / Typescript application I have:

authorize(policy: Policy) : Observable<boolean> {

  switch(policy) { 

    case Policy.Admin: 

      return zip(this.authenticationService.isSignedIn(), this.user$).pipe(
        map(([isSignedIn, user]: [boolean, UserModel]) => 
          isSignedIn 
          && 
          user?.claims?.some((claim: Claim) => claim.value === 'Admin'))
      );

user is of type Observable<UserModel> and isSignedIn is of type Observable<boolean, undefined>.

When I build the application I get the error:

Type 'Observable<boolean | undefined>' is not assignable to type 'Observable<boolean>'

I do not want to return undefined. I would prefer to return false instead of undefined.

How can I do this?

like image 803
Miguel Moura Avatar asked Apr 25 '26 06:04

Miguel Moura


1 Answers

End your map with a boolean ...

  const isSignedIn$ = this.authenticationService.isSignedIn();
  const user$ = this.user$;

  return zip(isSignedIn$, user$).pipe(
    map(([isSignedIn, user]: [boolean, UserModel]) => {
        const hasAdminRole = user?.claims?.some((claim: Claim) => claim.value === 'Admin');
        
        return !!(isSignedIn && hasAdminRole);
    })
  );
like image 73
Stavm Avatar answered Apr 28 '26 05:04

Stavm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!