如何解决 'null' 类型的参数无法分配给 'GoogleUser | undefined' 类型的参数

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

我正在尝试使用 gapi auth2 注册 google。以下代码在 google-sigin.service.ts 中,我在其中定义了函数登录和注销。我在 this.subject.next(null) 中收到错误错误是:

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

将您的 this.subject.next(null) 调用更改为 this.subject.next(undefined),或者将 subject = new ReplaySubject<gapi.auth2.GoogleUser>(1) 的类型签名扩展为 gapi.auth2.GoogleUser | null