Shubhendu
Shubhendu

Reputation: 51

Typescript Object is of type unknown

How to define the data type of value in the nodejs function written in typescript, so that error of Object is of type unknow can be solved ?

enter image description here

Upvotes: 1

Views: 1449

Answers (1)

Yuriy Vorobyov
Yuriy Vorobyov

Reputation: 775

You need to implement type for value. Try smth like this:

type valueType = {
  id: string,
  field1: number,
  field2: string,
}

const obj = {
  a: {
    id: '12',
    field1: 1,
    field2: 'asdas',
  }
}

for (const [key, value] of Object.entries(obj)) {
  const typedValue: valueType = value;
}

Upvotes: 3

Related Questions