title: Accessing different properties in a typescript union type
body: <p>I'm creating a function that handles objects from the database. I have two different data structures where the same property has a different name. I can't change that, so I have to handle it in JavaScript.<br>
  The objects have other differences, but that's not important to this function.<br>
  I want to use the same function for two different types of objects. Here's sample code demonstrating my problem:</p>
  
  <pre><code>interface TypeA {
      itemName: string;
  }
  
  interface TypeB {
      itemTitle: string;
  }
  
  function getItemName(item: TypeA | TypeB): string {
      let name = '';
  
      if (item.hasOwnProperty('itemName')) {
          name = item.itemName;
      } else {
          name = item.itemTitle;
      }
  
      return name;
  }
  </code></pre>
  
  <p>Of course, this code runs. But the IDE marks both the lines <code>name = item.itemName;</code> and <code>name = item.itemTitle;</code> as errors ("Property does not exist on type"), because both types do not have both properties.</p>
  
  <p>So, what's the proper typescript way to do this?</p>