我已经在public/index.html中加载了google maps API,在开发工具控制台中,我可以很好地记录window.google.maps。

但是TypeScript不知道它在那里,我得到这个错误,Property 'google' does not exist on type 'Window & typeof globalThis'.ts(2339),我如何告诉typescript它确实存在?
import React, { useRef } from "react";
function Map() {
const mapRef = useRef<HTMLDivElement>(null);
const google = window.google;
// ^ Error: Property 'google' does not exist on type 'Window & typeof globalThis'
const myLatlng = new google.maps.LatLng(-34.397, 150.644);
const mapOptions = {
zoom: 8,
center: myLatlng,
};
const map = new google.maps.Map(mapRef.current, mapOptions);
return (<div ref={mapRef}></div>);
}
export { Map };发布于 2020-04-30 06:56:55
我又想了想这件事。如果您还没有安装来自DefinitelyTyped的Google Map typings,我建议您这样做:
npm install --save @types/googlemaps。(如果您只想将它们用于自己的开发,则可以使用--save-dev。)
发布于 2020-09-21 11:11:27
谷歌建议使用带有此设置的ts.config.json。
https://developers.google.com/maps/documentation/javascript/using-typescript
{
"compilerOptions": {
"types": ["google.maps"]
}
}在项目的根目录中,您必须创建ts.config.json并添加该属性single property。下面是创建ts.config.json的方法:
npx tsc --inithttps://stackoverflow.com/questions/60986424
复制相似问题