title: How to associate and infer property types in typescript from given value
body: <p>I have the following example code that resembles the actual classes (I do not put the full class because is quite big.</p>
  <pre><code>type accepted = 'a' | 'b';
  
  type objects = A | B;
  
  class A {
      aprop: string[] = [];
      add(): void {}
  }
  
  class B {
      myprop: string = '';
      anotherprop: number = 1;
      add(): void {}
  }
  
  class C&lt;T extends objects&gt; {
      prop1: accepted;
      prop2: T;
      constructor (prop1: accepted, prop2: T) {
          this.prop1 = prop1;
          this.prop2 = prop2;
      }
  }
  
  const right1 = new C('a', new A());
  const right2 = new C('b', new B());
  const wrong1 = new C('a', new B()); //should not be possible
  
  </code></pre>
  <p>What I want is to make sure that whenever I instantiate a class C with certain value of <code>prop1</code> (a set of possible strings) the <code>prop2</code> shall be its correspondent class.</p>
  <p>In other words, I want to infer that <code>prop2</code> is class A or B when the value of <code>prop1</code> is <code>'a'</code> or <code>'b'</code>.</p>
  <p>In addition, a <a href="https://www.typescriptlang.org/play?#code/C4TwDgpgBAhgxnCZgQCZQLxQOQ21AHxwCNsBuAKAtEigHtiArCOYAZ0ygEFCoAhShTgAbGGw48A3hSizYYAE50wALihtgCgJYA7AOYBtALqdjlObFSoAFAEo1ANzpb0kgL4UPQ0eP5RpFgC24Eqq6pq6epzY5DJyMDp0wAAWEAqKymo6AK6BxGmcAIzm8VZ2js6uHl4iYhwAwgA8ACpQEAAeKDqoHAzMrGwAfP5xshlghWrwiMhoJWOhAExqzfNQcHQ6GgrZrHQKUNbjk7AISCioADRQ48tQzbYjFhYpWmwAdMecx2svyW+fJbfJZrapUDZbYBQbR6ZLAQqcHQQADuUHq1lw2GuSNRXDstkoEI00K0sOAi0RKLRGNI2KpfHxhM2xORSn0CKwOOpmLpqIZtgJUAA9EK2Mk6NlhOhElD8jc6OItMRhBAgA" rel="nofollow noreferrer">playground</a></p>