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?
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);
})
);