Last active
April 6, 2022 23:09
-
-
Save isaacbatst/352a13664cc4103cdb33b0133e5d58a3 to your computer and use it in GitHub Desktop.
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
enum PizzaFlavour { | |
CALABREZA = 'CALABREZA', | |
FRANGO_CATUPIRY = 'FRANGO_CATUPIRY', | |
NORDESTINA = 'NORDESTINA', | |
} | |
type PizzaFlavourKey = keyof typeof PizzaFlavour; | |
const pizzaFlavourPriceMap: Map<PizzaFlavourType, number> = new Map<PizzaFlavourKey, number>([ | |
[PizzaFlavour.CALABREZA, 10], | |
['FRANGO_CATUPIRY', 15], | |
// ['FEIJOADA', 20] // ERRO: O tipo '"FEIJOADA"' não pode ser atribuído ao tipo '"CALABREZA" | "FRANGO_CATUPIRY" | "NORDESTINA"'. | |
]) | |
// EXTRA: aplicando em objetos | |
const pizzaFlavorPriceObject: Record<PizzaFlavourType, number> = { | |
} | |
// ERRO: O tipo '{}' não tem as propriedades a seguir do tipo 'Record<"CALABREZA" | "FRANGO_CATUPIRY" | "NORDESTINA", number>': CALABREZA, FRANGO_CATUPIRY, NORDESTINA | |
// O Generic Record cria um objeto em que todas as chaves do enum são obrigatórias | |
// Se fizer sentido que as propriedades sejam opcionais | |
// Basta usar o Generic Partial | |
const pizzaFlavorPricePartialObject: Partial<Record<PizzaFlavourType, number>> = { | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment