I'm wanting to do something like this:
type Foo = {
name: string;
value: any;
}
const myFoos: Foo[] = [
{
name: "hello",
value: 1,
},
{
name: "world",
value: "bar"
}
];
function createObjectFromFooArray(foos: Foo[]) : Record<string, Foo> { //Need to change the return type
return foos.reduce((acc, cur) => {
return {
...acc,
[cur.name]: cur
}
}, {});
}
const objectFoos = createObjectFromFooArray(myFoos);
console.log(objectFoos.hello);
console.log(objectFoos.aaa); //Should error
That is - given an array of Foos, I want to create a mapped object of Foos, and I want that object to only contain indexes that were the name property of the original Foo array . How would I do this?