Cyr3xe
Cyr3xe

Reputation: 47

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

Im trying to create a Google SignIn with Firestorm. But to many things have chanced since the creation of the guide and i cant find an answer :(

So the Problem is:

export class AuthService {
  
  user$: Observable<User>; //This Observable can be NULL or undefined.

  constructor(
        private afAuth: AngularFireAuth,
        private afs: AngularFirestore,
        private router: Router
  ) {
    

    this.user$ = this.afAuth.authState.pipe(   //this one is not assignable to null or undefined
      switchMap(user => {
          // Logged in
        if (user) {
          return this.afs.doc<User>(`users/${user.uid}`).valueChanges();
        } else {
          // Logged out
          return of(null);
        }
      })
    )
    
   }
}

Im always getting this error:

Type 'Observable<User | null | undefined>' is not assignable to type 'Observable'. Type 'User | null | undefined' is not assignable to type 'User'. Type 'undefined' is not assignable to type 'User'.ts(2322)

But i cant figure out what to to.

Thats the original documetation:

https://fireship.io/lessons/angularfire-google-oauth/

Upvotes: 4

Views: 9231

Answers (1)

RobrechtVM
RobrechtVM

Reputation: 936

In your method you can possibly return null so you to have declare the user$ Observable as possibly null or undefined as well

  user$: Observable<User | null | undefined>;

Upvotes: 20

Related Questions