我已经在public/index.html
中加载了Google Maps Javascript API,在开发工具控制台中,我可以很好地登录window.google.maps
。
但是TypeScript不知道它在那里,并且出现此错误Property 'google' does not exist on type 'Window & typeof globalThis'.ts(2339)
我怎么知道它确实存在?
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 };
我考虑了更多。如果您尚未从DefinitelyTyped安装Google Map类型,则建议这样做:
nom install --save @types/googlemaps
。 (如果您只希望它们用于自己的开发,则为--save-dev
。