Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object is of type 'unknown' - forEach and map

Tags:

typescript

I am doing a loop on an Object using forEach or map. However, I am getting a type error on the variable that I use to loop. The error is Object is of type 'unknown'.

This occurs on val.name, val.title.

With .forEach

    Object.entries(data).forEach(
      ([, val]) => {
        newObject[val.name] = { // Object is of type 'unknown'
          title: val.title // Object is of type 'unknown'
        }}
      )

With .map

Object.entries(data).map(
  ([, val]) => {
    newObject[val.name] = { // Object is of type 'unknown'
      title: val.title // Object is of type 'unknown'
    }}
  )

I tried to specify a type on the variable val such as:

([, val: any]) => {..my code..}

but it does not accept a type in that position

like image 632
Magofoco Avatar asked Sep 05 '25 16:09

Magofoco


1 Answers

I found the same problem, finally I just replace [k, v] by (k, v: any)

Object.entries(obj).forEach((key, value: any) => {console.log(key, " => ", value);});
like image 146
Ibrahim Kelly Avatar answered Sep 07 '25 16:09

Ibrahim Kelly