I am using react with typescript 3.7.2: I have three components: "Menu Component", "Login User" and "Logged in".
In the "Menu Component" I have a "user" property, this property can be defined or null:
interface Props {
user: UserIF | null;
}
The "Menu Component" checks if the "user" property is defined. If it is not defined (null) then menu redirects to the component "Login User" otherwise it redirects to the "Logged in" component.
Now I would like to define the typescript definition in the components as follows:
"Login user":
interface Props {
user: UserIF | null;
}
"Logged in"
interface Props {
user: UserIF;
}
Typescript is now complaining that user can also be Null in the "Logged in" component. But Typescript doesn't know that the variable user can't be null in the "Logged in" component.
How can I tell typescript that I have only a defined user varaible in the "Logged in" component?
UserIF? Is it an object?user(in a reducer or so), you can just use partialuser: Partial<UserIf>;