As I work on implementing an authentication function for user roles using TypeScript and Firebase/Store, I've come across a peculiar issue related to type guards in async functions, especially with the use of .then()
.
Here is a brief snippet showcasing the problem (excerpted out of context for conciseness):
authUser: firebase.User | null;
...
if (authUser) {
this.user(authUser.uid)
.get()
.then(snapshot => {
const dbUser = snapshot.data();
if (dbUser && !dbUser.roles) dbUser.roles = {};
authUser = {
uid: authUser.uid,
email: authUser.email,
emailVerified: authUser.emailVerified,
providerData: authUser.providerData,
...dbUser,
};
next(authUser);
});
} else {
...
}
The TypeScript compiler raises a concern that in the
authUser = { uid: authuser.uid ... }
block, authUser
might be null
. This seems odd considering it's enclosed within the if
statement. Could this potential issue be linked to the asynchronous nature of the function?