Given a type that is an array of objects, how do I create a new type that is an object, where the keys are values of a specific property in the array objects, and the values of the object are the type of one of the object generics.
With JS code, it would look like this:
const obj = arr.reduce((acc, element) => ({
...acc,
[element.name]: element.bar
}))
So far I have this:
type ArrayToObject<Arr extends Array<CustomType<any>> = []> = {
[K in keyof Arr]: ExtractCustomTypeGeneric<Arr[K]>
}
type ExtractCustomTypeGeneric<A> = A extends CustomType<infer T> ? T : never;
type CustomType<T> = {
name: string,
foo: number,
bar: T
}
This gives me an array object (since K in keyof Arr are index numbers), whereas I would want the property keys to be name. Unfortunately something like [Arr[K]['name'] for K in keyof Arr] isn't something that works.