I'm attempting to retrieve a property from an object within an array using a specific index. Although I have checked for the object's existence, TypeScript is still warning that it might be null.
It's worth noting that in my project, "selectedIndex" is actually a state managed by React.
const arrayOfObjects: {bar:{baz: string; qux: string;}}[] = [{bar: {baz: 'value', qux: 'value'}}];
let selectedIndex: number = 0; // may vary
if (arrayOfObjects[selectedIndex].bar !== null) {
doSomethingWith(arrayOfObjects[selectedIndex].bar.baz);
// At this point, a typescript error may appear indicating that the "bar" Object could potentially be null
}
Any suggestions?