Here is a snippet of TypeScript code:
if (props.categories && props.categories.length > 0) {
// code...
}
props.categories is defined like this:
interface MyComponentProps {
categories?: string[];
}
I'm a little confused about the ? operator here...I would think that I could shorten my conditional to:
if (props.categories?.length > 0) {
// code...
}
But, TypeScript complains that "Object is possibly undefined". Why is this?