I can't be the first person coming across this, but my searches have not turned up any useful leads yet. Would greatly appreciate some expert TypeScript advice.
Say I have an array:
const fruits = ["Apple", "Orange", "Pear"];
and I want to define an object mapping each fruit to some fun facts about it:
interface Facts {
color: string,
typicalWeight: number
}
const fruitFacts: { [key: members of fruits]: Facts } = {
"Apple": { color: "green", typicalWeight: 150 }
//
}
How do I do that [key: members of fruits]
part?
Bonus: How do I enforce that my fruitFacts
object exhaust all the keys derived from the array as well, so that it specifies facts for Apples, Oranges, and Pears in the example above.