title: How can I get a typescript object property to accept a union of an array (of objects) or object?
body: <p>first stack overflow question, so hopefully I'm asking in an useful manner and apprecate all assistance :-)</p>
  <p>I have an interface that whose property 'location' requires flexibility as either an array of objects or an object itself. Currently my structure declaration is as follows:</p>
  <pre><code>export interface InitialValues {
    name: string;
    description: string;
    website: string;
    category: string;
    logo?: object;
    banner?: object;
    location: Location[] | Location;
  }
  
  interface Location {
    address: string;
    phone: string;
    lat: string;
    lng: string;
    availability: [
      {
        day: string;
        from: string;
        to: string;
      }
    ];
  }
  </code></pre>
  <p>Where:
  const values: InitialValues = {...}</p>
  <p>and later on depending on the value of a parameter variable (campaign: booelan), a defining of values.location as such:</p>
  <pre><code>if (campaign) {
        values.location.address = results[0].formatted_address;
        values.location.lat = latlng.lat;
        values.location.lng = latlng.lng;
      } else if (!campaign) {
        values.location[index].address = results[0].formatted_address;
        values.location[index].lat = latlng.lat;
        values.location[index].lng = latlng.lng;
      }
  </code></pre>
  <p>This of course isn't working out for me, and I'm getting errors that properties address, lat and lng do not exist on type Location | Location[]. However, removing Location from the union allows for the else if statements to clear error. My understanding is because unions are only valid for numbers, strings and boolean types.</p>
  <p>Hopefully, this is clear. Thanks!</p>