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

165
Visualizações
Value in Array is undefined TypeScript

I got an error in my foreach with an array:

function setRowData({ json }: NewType): void {
    // foreach key and value in json object
    // fill into an object
    // add object to array
    let tableRows: { id: string; key: string; de: string; it: string; }[] = [];
    Object.entries(json).forEach(([key, value]) => tableRows.push({
        id: key, key: key, de: value, it: (typeof value === 'string' ? value : "")
    }));
    setTableData(tableRows);
}

The error occurs on the line with the following content: id: key, key: key, de: value, it: (typeof value === 'string' ? value : "")

Does anyone know why the value variable called value inside my array is undefined?

In addition I post a photo of it and where the error occurs: enter image description here

This one is the description of the foreach, why is the second type in the array undefined? enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you use Object.entries() you extract an array of key-value tuples.

In this case the problem is that typescript cannot infer the type of values in the object, so it types it as unknown ([string, unknown][]).

object entries unknown

The error is telling you that you cannot assign unknown to string.

In order to have it typed as string you must either specify it when using Object.entries():

typed as string

const entries = Object.entries<string>(json);

Or you should specify in your NewType that json is an object with only string as values (using typescript's Record: Record<string, string>)

about 4 years ago · Juan Pablo Isaza Relatório
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