Search code examples
angularoauth-2.0google-api-js-client

How can I resolve Argument of type 'null' is not assignable to parameter of type 'GoogleUser | undefined'


I'm trying to sign up with google using gapi auth2. the following code is in google-sigin.service.ts in which I define the functions signin and signout. I get the error in this.subject.next(null) the error is:

Argument of type 'null' is not assignable to parameter of type 'GoogleUser | undefined'.

export class GoogleSiginService {

  private auth2!: gapi.auth2.GoogleAuth 
  private subject = new ReplaySubject<gapi.auth2.GoogleUser>(1)

  constructor() { 
    gapi.load('auth2',()=> {
     this.auth2 = gapi.auth2.init(
       {
         client_id:'184******'
       }
     )
    })
   
  }
  public sigin(){
    this.auth2.signIn({
    scope:'https://www.googleapis.com/auth/gmail.readonly'
    }).then(user => {
    this.subject.next(user)
    }).catch( ()=> {
     this.subject.next(null)
    })
  }
  public signOut()
  { this.auth2.signOut().then(()=>
    {
      this.subject.next(null)
    })

  }
  public observable(): Observable<gapi.auth2.GoogleUser>
  {
    return this.subject.asObservable()
  }
}

Solution

  • Either change your this.subject.next(null) calls to this.subject.next(undefined), or expand the type signature of subject = new ReplaySubject<gapi.auth2.GoogleUser>(1) to be gapi.auth2.GoogleUser | null.