title: Property type depending on type of other property
body: <p>So I'm testing TypeScript in how far I can take it and cannot seem to get past the following issue.
  How can I limit the type of property B when property A has some value?</p>
  <pre><code>// The type I want to declare
  type Bar&lt;T&gt; = {
      prop: keyof T; // Select a property of the type
      value: T[keyof T]; // Provide the value of that property, this currently does not work
  }
  
  // Some random interface
  interface Foo {
      id: number;
      name: string;
  }
  
  let bar: Bar&lt;Foo&gt; = {
      prop: &quot;name&quot;, // Selected Foo.name: string
      value: 9,     // Should only allow strings
  };
  </code></pre>
  <p>The property type of <code>value</code> in this case is <code>number | string</code>, but I would like to force it to string since the selected property <code>name</code> is of type <code>string</code>.</p>
  <hr />
  <p><strong>Notes</strong></p>
  <p>I can declare it this way, but the interface is a lot less appealing, clear and is more error prone: only one property should be selectable and you don't really know what is expected since property names are absent. Or I need to nest objects even further.</p>
  <pre><code>type Bar&lt;T&gt; = {
      prop: {
          [K in keyof T]?: T[K];
      }
  }
  
  let bar: Bar&lt;Foo&gt; = {
      prop: {
          name: 'yay', // string is forced now
      }
  };
  
  </code></pre>
  <ul>
  <li><a href="https://stackoverflow.com/questions/56949513/typescript-type-of-a-property-dependent-on-another-property-within-the-same-obj">Related question</a>. I guess this only works when the values are known at compile time.</li>
  </ul>