title: Conditional properties and switch statements in TypeScript
body: <p>I defined these types:</p>
  <pre class="lang-js prettyprint-override"><code>type ItemWithName = {
    type: &quot;ItemWithName&quot;;
    name: string;
    a: { name: string } ;
  };
  
  type ItemWithValue = {
    type: string;
    name: string;
    b: { value: number } ;
  };
  
  export type Item = ItemWithName | ItemWithValue;
  </code></pre>
  <p>And I have a function to extract a certain value from an object:</p>
  <pre class="lang-js prettyprint-override"><code>const extractValue = (item: Item) =&gt; {
    switch (item.type) {
      case &quot;ItemWithName&quot;:
        return item.a.name; // TS2339: Property 'a' does not exist on type 'Item'.
      case &quot;ItemWithValue&quot;:
        return item.b.value; 
    }
  };
  </code></pre>
  <p>But I get this error: <code>TS2339: Property 'a' does not exist on type 'Item'.</code></p>
  <p>I'm not entirely sure how to make TypeScript understand that if the type of the object is &quot;ItemWithValue&quot;, there's a property a.</p>
  <p>Note: this is a small abstraction of my particular issue, in reality I have several more of these types.</p>
  <p>The expected behaviour is that TypeScript detects that the item is of type <code>ItemWithName</code> if the object has a type value of <code>&quot;ItemWithName&quot;</code></p>