1

I have this object type

interface MyObject<T> {
  id: string
  value: T
}

And this array type

type MyArray = MyObject[] 

Now i need to declare a generic type that defines a new object based on ids and values in the MyObject's in the MyArray. In my mind in should look something like this, but i am not sure what to put inside the {{}}'s.

interface MyArrayObject<MyArray> {
    [id: {{MyArray[number].id}} ]: {{typeof MyArray[number].value}}
}

Example

const arr = [
  { id: "key1", value: 123 },
  { id: "key2", value: "abc" }
]

The resulting object contains all MyObject.ids and their respective MyObject.value.

const arrObject = {
  "key1": 123
  "key2": "abc" 
}

Is it even possible to type this?

Jonas Littau
  • 167
  • 2
  • 10

0 Answers0