Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
"Object is possibly undefined" when using Object.keys()

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!