I have a specific type of array that I need to convert into an indexed object with corresponding types.
interface IArray {
id: "message1" | "message2" | "message3";
message: string;
}
const myArray: IArray[] = [
{
id: "message1",
message: "Here is your message 1"
},
{
id: "message2",
message: "Here is your message 2"
}
]
To achieve this, I want to write a function that transforms the array into an object structured like this:
const myObject = {
message1: "Here is your message 1",
message2: "Here is your message 2"
}
The challenge I'm facing is that the resultant myObject
ends up having the generic type any
, but I intend for it to have keys indexed based on the id
type defined in the array.