Skip to content

Instantly share code, notes, and snippets.

@carlitorweb
Last active March 27, 2021 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlitorweb/9ce185c2f72510b5316c761dfe065963 to your computer and use it in GitHub Desktop.
Save carlitorweb/9ce185c2f72510b5316c761dfe065963 to your computer and use it in GitHub Desktop.
Use Enum as restricted key type - Typescript / React
// 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