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.
question from:
https://stackoverflow.com/questions/52085454/typescript-define-a-union-type-from-an-array-of-strings 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…