I tried this if in my react project.
if (
(role !== UserRoleEnum.Admin || role !== UserRoleEnum.Employee) &&
(project.state === ProjectState.online || project.state === ProjectState.onhold)
) { }
I get this error back
This condition will always return 'true' since the types 'UserRoleEnum.Admin' and 'UserRoleEnum.Employee' have no overlap. TS2367
How can i set this best up, that i got all in one if statement.
I want to check if
- user is not Admin or not Employee and
- project state is Online or OnHold
CodePudding user response:
If you want to check if the user is an Admin or an Employee, shouldn't you do something like this:
if ((role === UserRoleEnum.Admin) || (role === UserRoleEnum.Employee))
Notice that you were negating this comparison on your original code: role !== UserRoleEnum.Admin
This is probably why you got the compilation error, since the types don't seem to overlap, you will always receive a true
on this comparison:
if ((role !== UserRoleEnum.Admin) || (role !== UserRoleEnum.Employee))
The only way you would get a false
here would be if Admin
and Employee
have something in common that says that role
can't be neither. Otherwise, when role
is an Admin
it is not an Employee
, so you get true
and when it is an Employee
it is not an Admin
and you also get true
.
CodePudding user response:
I want to check if
- user is not Admin or not Employee and
- project state is Online or OnHold
The error message is specifically referring to the first condition in that set. Unless it's possible for any given user to be both an Admin and an Employee (and the compiler is telling you that it is not) then that condition will always be true
. Every user in the system is always not at least one of those two things.
According to comments in the question, you have confirmed that this is exactly the logic you want:
David: So did you mean "not admin and not employee"?
user1551496: No. I mean not admin OR not employee
Since your intended condition is always true, you have two options:
Remove the condition entirely and just always execute the logic in that
if
block; orJust remove the first part of the condition but still check the rest of it:
if (project.state === ProjectState.online || project.state === ProjectState.onhold) { }