Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
"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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda