title: How can I optionally embed one or more interfaces inside of a Typescript interface?
body: <p>I want to define a type <code>D</code> that requires all fields of interface <code>A</code>, but optionally also embeds the fields of interface <code>B</code>, <code>C</code> or both.</p>
  
  <p>Here is how I'm trying to define it:</p>
  
  <pre><code>interface A {
      a1: number;
      a2: number;
  };
  
  interface B {
      b1: number;
      b2: number;
  };
  
  interface C {
      c1: number;
      c2: number;
  };
  
  type D = A | (A &amp; B) | (A &amp; C) | (A &amp; B &amp; C);
  
  let d: D;
  d = {'a1': 1, 'a2': 2};
  if ('c1' in d) {
      d.c1 = 123;
  }
  </code></pre>
  
  <p>However, typescript doesn't seem to like this, and complains: <em>Property 'c1' does not exist on type 'never'.</em></p>
  
  <p>In case I've explained the above poorly, I want type D to be able to hold the following values:</p>
  
  <pre><code>{"a1": 1, "a2": 2}
  {"a1": 1, "a2": 2, "b1": 3, "b2": 4}
  {"a1": 1, "a2": 2, "c1": 5, "c2": 6}
  {"a1": 1, "a2": 2, "b1": 3, "b2": 4, "c1": 5, "c2": 6}
  </code></pre>
  
  <p>But not:</p>
  
  <pre><code>{"a1": 1} // missing field a2
  {"a1": 1, "a2": 2, "b2": 4} // missing field b1
  {"a1": 1, "a2": 2, "c1": 5} // missing field c2
  {"a2": 2, "b1": 3, "c2": 6} // missing fields a1, b2, c1
  </code></pre>
  
  <p>How can I do this in typescript?</p>