I'm having an issue with this piece of code, and assuring TypeScript that I know the location won't be undefined (as I'm iterating the People?).
On each line inside PeopleCard I'm getting Object is possibly 'undefined'.
I'm a little confused as if the Object is being iterated, I'm assuming it can't iterate
{people && Object.keys(people).map((id: string) => (
<PeopleCard
location={people[id]}
onDelete={() => openDeletePeopleModal(locations[id])}
onEdit={() => openEditPeopleDrawer(locations[id])}
onClick={() => history.push(`/todo/${todo.id}/people/${people[id].id}`)}
/>
))}
I get people from my Context, and I access it like so:
let { people } = selectedTodo;
TypeScript believes selectedTodo to be of type ITodo and people to be of type { [id: string]: Location } | undefined. Which seems correct to me.
My types are like so:
export type People = {
id: string;
peopleName: string;
address1: string;
address2?: string;
address3?: string;
address4?: string;
address5?: string;
town: string;
postcode: string;
country: string;
}
export type ITodo = {
id: string;
todoName: string;
todoType: string;
people?: { [id: string]: People };
};
I thought adding another check for undefined in might seal the deal, but whatever I've tried so far hasn't worked. I can just add ! after each one, but I'd rather not do that if it's easy to fix.