Typescriptでこんなエラー
(property) firebase.auth.Auth.currentUser: firebase.User | null
オブジェクトは 'null' である可能性があります。ts(2531)
auth.ts
app.auth().currentUser
.getIdToken(true) //赤下線
.then(function (idToken: any) {
console.log(idToken)
}).catch(function (error: any) {
alert(error)
});
これは app.auth().currentUser.getIdToken(true)
の値がnullの可能性があるからコンパイルエラー吐きだいしますよという事なので、 app.auth().currentUser.getIdToken(true)?
としてあげるとコンパイルが通る。
修正後
auth.ts
app.auth().currentUser
.getIdToken(true)?
.then(function (idToken: any) {
console.log(idToken)
}).catch(function (error: any) {
alert(error)
});