title: Types from both keys and values of object in Typescript
body: <p>I have two sets of string values that I want to map from one to the other as a constant object.  I want to generate two types from that mapping: one for keys and one for values.  </p>
  
  <pre class="lang-ts prettyprint-override"><code>const KeyToVal = {
      MyKey1: 'myValue1',
      MyKey2: 'myValue2',
  };
  </code></pre>
  
  <p>The keys are easy enough:</p>
  
  <pre class="lang-ts prettyprint-override"><code>type Keys = keyof typeof KeyToVal;
  </code></pre>
  
  <p>I'm having trouble getting a <em>compile-time</em> type for the values.  I thought maybe one of these would work:</p>
  
  <pre class="lang-ts prettyprint-override"><code>type Values = typeof KeyToVal[Keys];
  type Values&lt;K&gt; = K extends Keys ? (typeof KeyToVal)[K] : never;
  type Prefix&lt;
      K extends Keys = Keys, 
      U extends { [name: string]: K } = { [name: string]: K }
  &gt; = {[V in keyof U]: V}[K];
  </code></pre>
  
  <p>All of these just made <code>Values</code> to be <code>string</code>.  I also tried adapting the two answers to <a href="https://stackoverflow.com/questions/50822693/how-to-infer-typed-mapvalues-using-lookups-in-typescript">How to infer typed mapValues using lookups in typescript?</a>, but either I got my adaptations wrong, or the answers didn't fit my scenario in the first place.</p>