Enhanced with coding norms
I have obtained an array of fruit values:
const fruitsArray = ["apple", "banana", "peach"] as const;
Utilizing this array as a foundation, I generated types and values in the following manner:
type FruitsType = (typeof fruitsArray)[number]; // "apple" | "banana" | "peach"
type FruitsTypeMap = { [K in FruitsType]: K }; // {apple: "apple", banana: "banana", peach: "peach"}
const fruitsMap: FruitsTypeMap = fruitsArray.reduce(
(obj, key) => Object.assign(obj, { [key]: key }),
{} as FruitsTypeMap
); // {apple: "apple", banana: "banana", peach: "peach"}
All functionalities are operating smoothly.
My query is how to create FruitsType, FruitsTypeMap, and fruitsMap from fruitsArray using generics since I have various other arrays resembling fruitsArray.