Function createTypeMap

  • 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.

    • E = the const enum type (typeof Animal);
    • V = Identifies the valid values for the keys, this should include both the enum numeric and string key of the type. The resulting "Value" of each entry identifies the valid values withing the assignments.

    Type Parameters

    • E

      Identifies the enum type

    • T

      Identifies the return type that is being created via the mapping.

    Parameters

    • values: {
          [key in string | number | symbol]: [E[keyof E], T[keyof T]]
      }

      The values to populate on the new object

    Returns T

    A new frozen (immutable) object which contains a property for each key and value that returns the defined mapped value.

    Example

    // 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"