title: Typescript generic map with keys of type T[K] mapping back to T itself
body: <p>I have the following code:</p>
  <pre><code>type PartialRecord&lt;K extends keyof any, T&gt; = {
    [P in K]?: T;
  };
  
  export class CustomMaps {
    public static createInnerMap&lt;T, K extends keyof any&gt;(array: T[], customKey: keyof T): PartialRecord&lt;K, T&gt; {
      const innerMap: PartialRecord&lt;K, T&gt; = {};
      for (let i = 0; i &lt; array.length; ++i) {
        innerMap[array[i][customKey] as K] = array[i];
      }
      return innerMap;
    }
  }
  </code></pre>
  <p>It gives error: Conversion of type 'T[keyof T]' to type 'K' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.</p>
  <p>What it should do is this:</p>
  <pre><code>  class Car {
      private color: string;
      private wheelcount: number;
      constructor(color: string, wheelcount: number) {
        this.color = color;
        this.wheelcount = wheelcount;
      }
    }
    const redCar = new Car('red', 4);
    const blueCar = new Car('blue', 8);
    const yellowCar = new Car('yellow', 6);
    const colorMap = CustomMaps.createInnerMap&lt;Car, string&gt;([redCar, blueCar, yellowCar], 'color');
    colorMap['red'] // should return redCar instance
  
    const wheelMap = CustomMaps.createInnerMap&lt;Car, number&gt;([redCar, blueCar, yellowCar], 'wheelcount');
    wheelMap[8] // should return blueCar instance
  </code></pre>
  <p>Without casting innerMap[array[i][customKey]] to unknown, how can I ensure that array[i][customKey] will return a type that can be used for indexing (specifically the type I substitute for K)?</p>