title: TypeScript: Record<> where value depends on key
body: <p>I want to write something like this:</p>
  
  <pre><code>const structs: Record&lt;T extends StructureConstant,Array&lt;ConcreteStructure&lt;T&gt;&gt;&gt; = Object.create(null);
  </code></pre>
  
  <p>Where <code>StructureConstant</code> is a union of constants, and <code>ConcreteStructure</code> is basically a <a href="https://github.com/screepers/typed-screeps/blob/97948a482403d769501682a16c4d2708e758168b/dist/index.d.ts#L5404" rel="nofollow noreferrer">type lookup</a>.</p>
  
  <p>Is this possible in TypeScript? I can't figure out how to make the value dependent on the key.</p>
  
  <hr>
  
  <p>Here's a complete example:</p>
  
  <pre><code>type StructureConstant = "road" | "tower";
  
  interface Road {
      a: "i am a road",
  }
  
  interface Tower {
      b: "i be towerin"
  }
  
  type ConcreteStructure&lt;T extends StructureConstant&gt; = T extends "road"
      ? Road
      : T extends "tower"
      ? Tower
      : never;
  
  type StuctureRecord&lt;T extends StructureConstant&gt; = Record&lt;T, Array&lt;ConcreteStructure&lt;T&gt;&gt;&gt;
  
  const structs: StuctureRecord&lt;StructureConstant&gt; = Object.create(null); 
  
  const roads = structs["road"]; // should be Array&lt;Road&gt;|undefined
  
  console.log(roads[0].a); // should work (if structs was populated)
  </code></pre>
  
  <p><a href="https://www.typescriptlang.org/play/#code/C4TwDgpgBAysBOBXAxsR8IGED2A7AzsAIa7BQC8UARPNkQCZVQA+1w2A7hPFQNwBQ-AJaluAMyLJoAJTr0oAb35QVUIgC5qQtQFs1UWgyoAafgF9BI4OMnQAKp26LlqgEaaq219HZd4IqnNBUEgoHFxkDGs4JFR0CAAeOygIAA9rXHp8WAQUNAxwwhJgAD4KKGS0jKzqQ0YXFQB+KFkGBqhNSvSITOyqX25A1Shmhz92zVwIADduAX4Q6Dg8+OkIZGx4eiSU7t6c2PysPCLSMso1ja2k4ygAQXh4IhAE8MiIaNy4jCSSv8ENgQyIRDvhNMtvhBLpttjEVgUTsQzuUAPKuABW62AADp3kRrAAKXCIAA2JIAlLwoADEQY5NlKCC8vgANo0ORUAC680B+GwJIg2JJ2AA5gS6qyAAyc7FESlAA" rel="nofollow noreferrer">TS playground</a></p>