I am currently developing a small utility that generates typesafe remapped object/types of compound enums. The term "compound" in this context refers to the enum (essentially a const object) key mapping to another object rather than a numeric or literal value. Here is a playground with the code you can check out.
type CompoundRecord<T> = Record<keyof T, Record<keyof T[keyof T], T[keyof T][keyof T[keyof T]]>>;
const enumKeyName = '$$key';
type KeyOfEnumValue<T> = keyof T[keyof T];
type KeyOfEnumKey = typeof enumKeyName;
type KeyOf<T> = KeyOfEnumKey | KeyOfEnumValue<T>
export const remapEnumLike = <
T extends {},
IKey extends KeyOf<T>,
OKey extends KeyOf<T>,
>(
enumerable: T,
inKey: IKey,
outKey: OKey,
) => {
const remappedEnum = Object.fromEntries(
(Object.entries(enumerable) as Array<[keyof T, T[keyof T]]>).map(([key, value]) => {
return [inKey === enumKeyName ? key : value[inKey as KeyOfEnumValue<T>], outKey === enumKeyName ? key : value[outKey as KeyOfEnumValue<T>]];
})
);
return remappedEnum as RemappedEnumLike__wip<T, IKey, OKey> //RemappedEnumLike
};
// Additional code and comments are here, feels free to check them out!
If you have any suggestions or feedback on how to enhance this implementation to make it more efficient or reliable, please share! Thank you!