Skip to content

Instantly share code, notes, and snippets.

@rahmatrhd
Created April 29, 2019 07:23
Show Gist options
  • Save rahmatrhd/d4fb76bce84ba0527dc9544f2ae6b389 to your computer and use it in GitHub Desktop.
Save rahmatrhd/d4fb76bce84ba0527dc9544f2ae6b389 to your computer and use it in GitHub Desktop.
How to make an object type with the keys are members of an enum
enum PrimaryColor {
Blue,
Red,
Yellow,
}
type ColorRecipe = {
[T in PrimaryColor]: number
}
const orangeRecipe: ColorRecipe = {
[PrimaryColor.Blue]: 0,
[PrimaryColor.Red]: 0.5,
[PrimaryColor.Yellow]: 0.5,
}
const greenRecipe: ColorRecipe = {
[PrimaryColor.Blue]: 0.5,
[PrimaryColor.Red]: 0,
[PrimaryColor.Yellow]: 0.5,
}
const purpleRecipe: ColorRecipe = {
[PrimaryColor.Blue]: 0.5,
[PrimaryColor.Red]: 0.5,
[PrimaryColor.Yellow]: 0,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment