Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
Typescript type

I'm trying to loop through an object which holds a number of properties and the value is always an array of numbers. When I try to run the code below I get the error:

property 'length' does not exist on type 'unknown'

When using Typescrip how do I assign a types when the object key/value pair is destructured in this way?

Thanks

  for (const [key, value] of Object.entries(object)) {
      console.log(value.length)
   }

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can add a type to the object beforehand.

const object: Record<string, number[]> = {}

for (const [key, value] of Object.entries(object)) {
  console.log(value.length)
}

Read more about the Record utility type here

You can also type cast the object instead.

for (const [key, value] of Object.entries(object as Record<string, number[]>)) {
  console.log(value.length)
}

See all the examples in TS playground here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda