I have a unique challenge of mapping two sets of string values from one constant object to another. The goal is to generate two distinct types: one for keys and one for values.
const KeyToVal = {
MyKey1: 'myValue1',
MyKey2: 'myValue2',
};
Deriving the type for keys is straightforward:
type Keys = keyof typeof KeyToVal;
However, I am facing difficulty in defining a compile-time type for the values. Various attempts such as the following were made:
type Values = typeof KeyToVal[Keys];
type Values<K> = K extends Keys ? (typeof KeyToVal)[K] : never;
type Prefix<
K extends Keys = Keys,
U extends { [name: string]: K } = { [name: string]: K }
> = {[V in keyof U]: V}[K];
All these approaches resulted in Values
being inferred as string
. Adapting solutions from similar questions like the one at How to infer typed mapValues using lookups in typescript? did not yield the desired outcome, indicating either misapplication of those solutions or lack of alignment with my specific scenario.