I created an enum named FoodTypes:
enum FoodTypes {
Meat,
Veggie,
}
Then I declared an interface named Recipe that contains arrays of objects of FoodTypes.
interface Recipe {
[FoodTypes.Meat]: [
{ name: 'Beef', val: 120 },
{ name: 'Pork', val: 200 },
],
[FoodTypes.Veggie]: [
{ name: 'Mushroom', val: 30 },
{ name: 'Lettuce', val: 10 },
],
};
How do I access a specific value in the interface?
For example, I'd like to get the val of Beef.
// should be 120
Thanks so much!
Edit: @captain-yossarian For example, if I want to pass in the value later in a function as it changes dynamically (using React Redux in front-end), would I be able to do so? I know that the following wouldn't work since I'm referring to a type:
function getBeefVal(recipe: Recipe) {
return "Beef Val: " + Recipe[FoodTypes.Meat][0]['val'];
}
FoodTypes.Meat's two properties to have specific literal values? It seems like you just want a runtime lookup table, not a type-level interface.interface IFoodAttribute {name: string; value: number }&interface IFood { [FoodTypes.Meat]: IFoodAttribute[] }. The specific literal values will be passed in dynamically in Redux. If there's a better way to do things, please let me know as I'm quite new to TS. Thanks!