title: Typescript: Defining an interface for an array of objects from a google JSON?
body: <p>I'm looping over my data that's returned from a Google API to receive the city and state, but I keep receiving this error</p>
  <pre><code>Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Geocode'.
    No index signature with a parameter of type 'string' was found on type 'Geocode'.
  </code></pre>
  <p>Which appears when I try to loop the data:</p>
  <pre><code>data[key].address_components.forEach
  </code></pre>
  <p>So far I looked at &quot;Indexing objects&quot; and using a interface, but I'm still not sure how to use them</p>
  <p>Here is my code so far:</p>
  <pre><code>type Geocode = {
    data?: google.maps.GeocoderResult | null;
    error: string | null;
  };
  
  export interface CityState {
    city?: string;
    state?: string;
  }
  
  export const parseForCityAndState = (data: Geocode): CityState =&gt; {
    const cityState = {
      city: '',
      state: '',
    };
  
    if (data) {
      for (const key in data) {
        data[key].address_components.forEach((ele: {types: string | string[]; short_name: string}) =&gt; {
          if (ele.types.includes('sublocality') || ele.types.includes('locality')) {
            cityState.city = ele.short_name;
          }
  
          if (ele.types.includes('administrative_area_level_1')) {
            cityState.state = ele.short_name;
          }
        });
      }
    }
  
    return cityState;
  };
  </code></pre>
  <p>Here is how the google JSON returns the data:</p>
  <pre><code>{
      &quot;data&quot;: {
          &quot;address_components&quot;: [
              {
                  &quot;long_name&quot;: &quot;254&quot;,
                  &quot;short_name&quot;: &quot;254&quot;,
                  &quot;types&quot;: [
                      &quot;street_number&quot;
                  ]
              },
              {
                  &quot;long_name&quot;: &quot;Broadway&quot;,
                  &quot;short_name&quot;: &quot;Broadway&quot;,
                  &quot;types&quot;: [
                      &quot;route&quot;
                  ]
              },
              {
                  &quot;long_name&quot;: &quot;Manhattan&quot;,
                  &quot;short_name&quot;: &quot;Manhattan&quot;,
                  &quot;types&quot;: [
                      &quot;political&quot;,
                      &quot;sublocality&quot;,
                      &quot;sublocality_level_1&quot;
                  ]
              },
              {
                  &quot;long_name&quot;: &quot;New York&quot;,
                  &quot;short_name&quot;: &quot;New York&quot;,
                  &quot;types&quot;: [
                      &quot;locality&quot;,
                      &quot;political&quot;
                  ]
              },
              {
                  &quot;long_name&quot;: &quot;New York County&quot;,
                  &quot;short_name&quot;: &quot;New York County&quot;,
                  &quot;types&quot;: [
                      &quot;administrative_area_level_2&quot;,
                      &quot;political&quot;
                  ]
              },
              {
                  &quot;long_name&quot;: &quot;New York&quot;,
                  &quot;short_name&quot;: &quot;NY&quot;,
                  &quot;types&quot;: [
                      &quot;administrative_area_level_1&quot;,
                      &quot;political&quot;
                  ]
              },
              {
                  &quot;long_name&quot;: &quot;United States&quot;,
                  &quot;short_name&quot;: &quot;US&quot;,
                  &quot;types&quot;: [
                      &quot;country&quot;,
                      &quot;political&quot;
                  ]
              },
              {
                  &quot;long_name&quot;: &quot;10007&quot;,
                  &quot;short_name&quot;: &quot;10007&quot;,
                  &quot;types&quot;: [
                      &quot;postal_code&quot;
                  ]
              }
          ],
          &quot;formatted_address&quot;: &quot;254 Broadway, New York, NY 10007, USA&quot;,
          &quot;geometry&quot;: {
              &quot;location&quot;: {
                  &quot;lat&quot;: 40.7129032,
                  &quot;lng&quot;: -74.0063033
              },
              &quot;location_type&quot;: &quot;ROOFTOP&quot;,
              &quot;viewport&quot;: {
                  &quot;south&quot;: 40.7115542197085,
                  &quot;west&quot;: -74.0076522802915,
                  &quot;north&quot;: 40.7142521802915,
                  &quot;east&quot;: -74.00495431970849
              }
          },
          &quot;place_id&quot;: &quot;ChIJzwO0CyJawokRoSRPRCTv9II&quot;,
          &quot;plus_code&quot;: {
              &quot;compound_code&quot;: &quot;PX7V+5F New York, NY, USA&quot;,
              &quot;global_code&quot;: &quot;87G7PX7V+5F&quot;
          },
          &quot;types&quot;: [
              &quot;street_address&quot;
          ]
      },
      &quot;error&quot;: null
  } 
  </code></pre>