Last active
March 27, 2021 16:31
-
-
Save carlitorweb/9ce185c2f72510b5316c761dfe065963 to your computer and use it in GitHub Desktop.
Use Enum as restricted key type - Typescript / React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// type.ts | |
enum Ingredient { | |
salad = "salad", | |
bacon = "bacon", | |
cheese = "cheese", | |
meat = "meat", | |
breadBottom = "bread-bottom", | |
breadTop = "bread-top", | |
} | |
// Optional properties | |
type variant = Partial<Record<Ingredient, number>>; | |
// No optional properties | |
type ingredients = Record<Ingredient, number>; | |
export default ingredients | |
---------------------------------- | |
import Ingredients from "./type" | |
class BurgerBuilder extends Component { | |
ingredients: Ingredients = { | |
salad: 1, | |
bacon: 1, | |
cheese: 2, | |
meat: 2, | |
} | |
state = { | |
ingredients: this.ingredients, | |
}; | |
/* rest of the code */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment