简体   繁体   English

打字稿错误:环境模块声明只允许在文件的顶层

[英]Typescript Error: An ambient module declaration is only allowed at the top level in a file

Currently trying to fetch data from a google service.目前正在尝试从谷歌服务获取数据。 I'm not sure how I should handle this error when declaring global .我不确定在声明global时应该如何处理这个错误。

Link to the original code snippet: https://stackblitz.com/edit/github-mge1vg?file=index.ts链接到原始代码片段: https ://stackblitz.com/edit/github-mge1vg?file=index.ts

How would solve this and why am I getting the error.如何解决这个问题,为什么我会收到错误。
This part of the code throws an error:这部分代码会抛出错误:

An ambient module declaration is only allowed at the top level in a file.

 declare global {
    interface Window {
      initMap: () => void;
    }
  }
  window.initMap = initMap;

Here is the whole code:这是整个代码:

import {useEffect, useState} from 'react';

type Geocode = {
  data?: google.maps.GeocoderResult | null;
  loading: boolean;
  error: string | null;
};

export const useReverseGeoCoding = ({latitude, longitude}: {latitude: number; longitude: number}): Geocode => {
  const [geocode, setGeocode] = useState<Geocode>({loading: false, error: null});

  useEffect(() => {
    const geocodeLatLng = async (geocoder: google.maps.Geocoder) => {
      /// Hard Coded for now
      const latlng = {
        lat: 52.509865,
        lng: -0.118092,
      };

      geocoder
        .geocode({location: latlng})
        .then(response => {
          if (response.results[0]) {
            console.log('response:', response);
            setGeocode({data: response.results[0], loading: false, error: null});
          } else {
            console.log('No results found');
          }
        })
        .catch(error => console.log('Geocoder failed due to: ' + error));
    };
    const initMap = (): void => {
      const geocoder = new google.maps.Geocoder();
      geocodeLatLng(geocoder);
    };
    initMap();
  }, [latitude, longitude]);

  declare global {
    interface Window {
      initMap: () => void;
    }
  }
  window.initMap = initMap;

  return geocode;
};

Moving移动

declare global {
  interface Window {
    initMap: () => void;
  }
}

Outside of the main function works, then calling在主函数之外工作,然后调用

  const initMap = () => {
    const geocoder = new google.maps.Geocoder();
    setGeocoder(geocoder);
  };
  window.initMap = initMap;

Within the file you will use the hook, and add a script tag to initialize the Google API在文件中,您将使用钩子,并添加一个脚本标签来初始化 Google API

 <script
      src={`https://maps.googleapis.com/maps/api/js?key=${your API key}&callback=initMap&v=weekly&libraries=geocoder`}
      defer
    ></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿错误:找不到模块“react-dnd”的声明文件 - Typescript Error: Could not find a declaration file for module 'react-dnd' typescript 错误不允许作为变量声明名称.ts - typescript error is not allowed as a variable declaration name.ts 错误TS7016:找不到模块“”的声明文件。 &#39;&#39;隐含一个&#39;any&#39;类型。 TypeScript 2.0 - error TS7016: Could not find a declaration file for module ''. '' implicitly has an 'any' type. TypeScript 2.0 在反应中找不到模块错误的声明文件 - Could not find a declaration file for module error in react 在具有“循环”依赖性的文件的顶级范围中访问导入的模块? - Access imported module in top level scope of file with 'circular' dependency? 流星1.5.2.2-“每个模块只允许一个默认导出”错误 - Meteor 1.5.2.2 - “Only one default export allowed per module” error Typescript 找不到声明文件 - Typescript cant find declaration file 如何找到我的打字稿/反应模块的声明? - How to find declaration for my typescript/react module? 错误:“无法找到模块&#39;react-search-input&#39;的声明文件” - Error: “Could not find a declaration file for module 'react-search-input'” 我收到错误消息“找不到模块‘react’的声明文件” - I'm getting an error of "Could not find a declaration file for module 'react'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM