A new frozen (immutable) object which contains a property for each key and value that returns the defined mapped value.
// Create a strongly types map
const animalFamilyMap = createTypeMap<typeof Animal, {
// Defined the enum lookups
[Animal.Dog]: "Canidae",
[Animal.Cat]: "Felidae",
[Animal.Butterfly]: "Papilionidae",
[Animal.Bear]: "Ursidae",
// Defined Named reference
Dog: "Canidae",
Cat: "Felidae",
Butterfly: "Papilionidae",
Bear: "Ursidae",
}>({
Dog: [ Animal.Dog, "Canidae"],
Cat: [ Animal.Cat, "Felidae"],
Butterfly: [ Animal.Butterfly, "Papilionidae"],
Bear: [ Animal.Bear, "Ursidae"]
});
// You end up with a strongly types result for each value
animalMap.Dog === "Canidae"; // true with typeof animalMap.Dog is (const) "Canidae"
animalMap[0] === "Canidae"; // true with typeof animalMap[0] is "Canidae"
animalMap["Dog"] === "Canidae"; // true with typeof animalMap["Dog"] is "Canidae"
animalMap.Cat === "Felidae"; // true with typeof animalMap.Cat is "Felidae"
animalMap[1] === "Felidae"; // true with typeof animalMap[1] is "Felidae"
animalMap["Cat"] === "Felidae"; // true with typeof animalMap["Cat"] is "Felidae"
or using an interface to define the direct string mappings
interface IAnimalFamilyMap {
Dog: "Canidae",
Cat: "Felidae",
Butterfly: "Papilionidae",
Bear: "Ursidae"
}
// Create a strongly types map
const animalFamilyMap = createTypeMap<typeof Animal, IAnimalFamilyMap & {
// Defined the enum lookups
[Animal.Dog]: "Canidae",
[Animal.Cat]: "Felidae",
[Animal.Butterfly]: "Papilionidae",
[Animal.Bear]: "Ursidae"
}>({
Dog: [ Animal.Dog, "Canidae"],
Cat: [ Animal.Cat, "Felidae"],
Butterfly: [ Animal.Butterfly, "Papilionidae"],
Bear: [ Animal.Bear, "Ursidae"]
});
// You also end up with a strongly types result for each value
animalMap.Dog === "Canidae"; // true with typeof animalMap.Dog is (const) "Canidae"
animalMap[0] === "Canidae"; // true with typeof animalMap[0] is "Canidae"
animalMap["Dog"] === "Canidae"; // true with typeof animalMap["Dog"] is "Canidae"
animalMap.Cat === "Felidae"; // true with typeof animalMap.Cat is "Felidae"
animalMap[1] === "Felidae"; // true with typeof animalMap[1] is "Felidae"
animalMap["Cat"] === "Felidae"; // true with typeof animalMap["Cat"] is "Felidae"
Create a strongly types map object which contains both the perperty key and value which both map to the requested mapValue, E[key] => mapValue and E[value] => mapValue.