Replies: 1 comment
-
it's probably not the recommended approach, but I like to use an function assert(condition: unknown, message: string): asserts condition {
if (!condition) throw new Error(message)
}
const selectNullableUser = (state: RootState) => state.user.user;
const selectUser = (state: RootState) => {
const user = selectNullableUser(state);
assert(user, "Only call selectUser if you know user is defined!");
return user; // now narrowed
}
const selectUserId = (state: RootState) => selectUser(state).id |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I use React with Redux and TypeScript
I am writing an application where all routes are protected by authentication except the user login page.
Here is a slice that handles the current user.
The issue that I have is that initially, the user is unknown, but it is known after the user is authenticated,so in 99% of places
and I don't want to handle the possible user being null in all components. The protected route should redirect to the login page in this case, but I still have an issue where the user type could be null.
Could you please help me organize this better?
in addition, I have the limitation that the user is being used in other slices
Beta Was this translation helpful? Give feedback.
All reactions