Given this code:
interface Obj {
array?: string[]
}
function fuu(obj: Obj) {
const { array } = obj
if (array?.length >= 1) {
// ...
}
}
Why would it still say 'array.length' is possibly 'undefined'.
When the whole purpose of the ?
is to check if it's undefined
or not.
I'm assuming it's because the operator >= 1
needs a defined value.
Is there a way other than doing:
if (array && array?.length >=1)
, or const { array = [] } = obj
?
The comments are right, the answer is in the question.
Either
if (array && array?.length >=1)
or
const { array = [] } = obj
Are sufficient alternatives.