Typescript error This condition will always return 'true' since the types have no overlap
Consider the standalone expression:
(this.frType!="Child" || this.frType!="Infant")
If frType
is Child
, the second part will be true, so the expression will evaluate to true
. If frType
is Infant
, then the first part will be true, so the expression will evaluate to true
. If frType
is neither Child
nor Infant
, then the first part will be true, and the expression will, again, evalute to true
- the logic is faulty, it'll always resolve to true
.
(If you add additional ||
conditions for Grandchild
and Cousin
, the same thing keeps happening - it'll always resolve to true
)
Either use &&
instead:
|| (age<=5 && (
this.frType!="Child"
&& this.frType!="Infant"
&& this.frType!="Grandchild"
&& this.frType!="Cousin"
))
Or, to make the logic easier to follow, you might consider using an array, and use .includes
:
const kidsFiveAndUnder = ['Child', 'Infant', 'Grandchild', 'Cousin'];
// ...
|| (age <= 5 && !kidsFiveAndUnder.includes(this.frType))
Maybe i can help someone with this.
In my case the error was triggered by:
*ngIf="fooArray.length === 0"
so i modified it to be:
*ngIf="fooArray.length < 1"
Makes no sense to me, but it works.