xhon_
xhon_

Reputation: 27

Type 'Observable<User | null>' is not assignable to type 'Observable<User>'

So here is the authService.ts :

 user$: Observable<firebase.User> 

 constructor(private afAuth: AngularFireAuth) { 
 this.user$ = afAuth.authState ;  //<-------- here's the problem
  }```                                                          
                                     

Upvotes: 2

Views: 4845

Answers (3)

smit agravat
smit agravat

Reputation: 295

I solved this as below

  1. First I add .map<firebase.User>()

  2. then I add <firebase.User | null>

This gave me error on .map method in VS Code but on this.user$ = afAuth.authState; error was removed.

So I saved file and removed .map.

Upvotes: 0

Shivam Rajput
Shivam Rajput

Reputation: 11

I am using vanilla script so I had to made custom observable to achieve that

 this.userob=new Observable(observe=>{
auth.onAuthStateChanged(x=>{                          
  observe.next(x);
})

})

Upvotes: 1

BizzyBob
BizzyBob

Reputation: 14740

It depends on your intention, do you want to allow null?

If so, just change the typing on your user$:

 user$: Observable<firebase.User|null> 

If you don't want null, you could filter out null emissions:

this.user$ = afAuth.authState.pipe(
   filter(u => !!u)
)

Upvotes: 2

Related Questions