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

189
Views
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 answers
Answer question

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 Report
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!