I check if array includes the value this way:
testArray.includes(value)
I need to do the same action if array is empty OR it includes the value. So I have this condition:
if (testArray == [] || testArray.includes(value)) {
// do something
}
But typescript is trying to execute the second part of this condition even if testArray == []. So I receive an error testArray.includes is underfined.
I understand that I can fix it this way:
if (testArray == []) {
// do something
} else if (testArray.includes(value)) {
// do the same thing
}
But it doesn't look nice. Is there a way to put it in one if?
testArray is an openapi query parameter, if it's important.
testArray == []will always be false. See: Why doesn't equality check work with arrays and Why isn't [1,2,3] equal to itself in Javascript? Moreover, if you gettestArray.includes is underfinedthen you don't have an empty array, you have some sort of value that is not an array. Likely an object.