Google 的地点自动完成地址表单 - 使用 any 类型索引 GeocoderAddressComponent 时出现 Typescript 错误

问题描述

运行以下内容

npm --version
7.4.0
 nvm --version
0.37.2
node --version
v15.6.0
tsc -v 
Version 4.1.3

lsb_release -a
No LSB modules are available.
distributor ID: Ubuntu
Description:    Ubuntu 20.04 LTS
Release:        20.04
Codename:       focal

我使用他们的示例命令设置项目:

npx @googlemaps/js-samples init places-autocomplete-addressform DESTINATION_FOLDER

以下来源是 Google 的地点自动完成地址表单示例代码https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform

// This sample uses the Autocomplete widget to help the user select a
// place,then it retrieves the address components associated with that
// place,and then it populates the form fields with those details.
// This sample requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script
// src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

let placeSearch: google.maps.places.PlacesService;
let autocomplete: google.maps.places.Autocomplete;

const componentForm = {
  street_number: "short_name",route: "long_name",locality: "long_name",administrative_area_level_1: "short_name",country: "long_name",postal_code: "short_name",};

function initAutocomplete() {
  // Create the autocomplete object,restricting the search predictions to
  // geographical location types.
  autocomplete = new google.maps.places.Autocomplete(
    document.getElementById("autocomplete") as HTMLInputElement,{ types: ["geocode"] }
  );

  // Avoid paying for data that you don't need by restricting the set of
  // place fields that are returned to just the address components.
  autocomplete.setFields(["address_component"]);

  // When the user selects an address from the drop-down,populate the
  // address fields in the form.
  autocomplete.addListener("place_changed",fillInAddress);
}

function fillInAddress() {
  // Get the place details from the autocomplete object.
  const place = autocomplete.getPlace();

  for (const component in componentForm) {
    (document.getElementById(component) as HTMLInputElement).value = "";
    (document.getElementById(component) as HTMLInputElement).disabled = false;
  }

  // Get each component of the address from the place details,// and then fill-in the corresponding field on the form.
  for (const component of place.address_components as google.maps.GeocoderAddressComponent[]) {
    const addresstype = component.types[0];

    if (componentForm[addresstype]) {
      const val = component[componentForm[addresstype]];
      (document.getElementById(addresstype) as HTMLInputElement).value = val;
    }
  }
}

// Bias the autocomplete object to the user's geographical location,// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition((position) => {
      const geolocation = {
        lat: position.coords.latitude,lng: position.coords.longitude,};
      const circle = new google.maps.Circle({
        center: geolocation,radius: position.coords.accuracy,});
      autocomplete.setBounds(circle.getBounds());
    });
  }
}

我在 VS Code 上遇到以下错误

ERROR in /home/aamarin/dev/testing/src/index.ts
./src/index.ts
[tsl] ERROR in /home/aamarin/dev/testing/src/index.ts(69,9)
      TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ street_number: string; route: string; locality: string; administrative_area_level_1: string; country: string; postal_code: string; }'.
  No index signature with a parameter of type 'string' was found on type '{ street_number: string; route: string; locality: string; administrative_area_level_1: string; country: string; postal_code: string; }'.

ERROR in /home/aamarin/dev/testing/src/index.ts
./src/index.ts
[tsl] ERROR in /home/aamarin/dev/testing/src/index.ts(70,19)
      TS7053: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'GeocoderAddressComponent'.

ERROR in /home/aamarin/dev/testing/src/index.ts
./src/index.ts
[tsl] ERROR in /home/aamarin/dev/testing/src/index.ts(70,29)
      TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ street_number: string; route: string; locality: string; administrative_area_level_1: string; country: string; postal_code: string; }'.
  No index signature with a parameter of type 'string' was found on type '{ street_number: string; route: string; locality: string; administrative_area_level_1: string; country: string; postal_code: string; }'.

我做了一些谷歌搜索,找到了一些例子,建议在索引 componentForm 时添加更多类型信息,因为认情况下键是字符串(我认为)并且值也是字符串。所以我通过引用 Enforcing the type of the indexed members of a Typescript object?

修改了以下内容
const componentForm : { [key: string]: string } = 

但是,我现在看到以下错误

ERROR in /home/aamarin/dev/testing/src/index.ts
./src/index.ts
[tsl] ERROR in /home/aamarin/dev/testing/src/index.ts(70,19)
      TS7053: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'GeocoderAddressComponent'.

它特别抱怨这个部分

const addresstype = component.types[0];

if (componentForm[addresstype]) {
  const val = component[componentForm[addresstype]];
  (document.getElementById(addresstype) as HTMLInputElement).value = val;
}

我不知道如何索引 GeocoderAddressComponent,因为接口是两个字符串和一个字符串数组 https://developers.google.com/maps/documentation/javascript/reference/geocoder#GeocoderAddressComponent。任何信息都会有所帮助。谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)