'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
  }```                                                          
                                     


Solution 1:[1]

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)
)

Solution 2:[2]

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

})

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Shivam Rajput
Solution 3 YakovL