3

I have loaded the google maps Javascript API in the public/index.html and in dev tools console I can log window.google.maps just fine.

enter image description here

But TypeScript doesn't know its there and I get this error Property 'google' does not exist on type 'Window & typeof globalThis'.ts(2339) How do I tell typescript it does exist?

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 };
4
  • declare const google Commented Apr 2, 2020 at 8:52
  • Hey Bill, I have the same problem, could you please tell me how did you resolve the problem? I've tried the solution from @ritaj but it doesn't work. Thanks Commented Apr 10, 2020 at 20:00
  • it's still unresolved but I plan to look at google-map-react t see how they did it Commented Apr 11, 2020 at 2:05
  • 1
    What does your line in index.html look like for loading the script? FWIW, for Google Maps and basic React, if you're using node, try installing the typings from Definitely Typed: npm install --save[-dev] @types/googlemaps. Those helped me a lot in doing the actual typings. Commented Apr 29, 2020 at 22:51

2 Answers 2

4

google recommends to have ts.config.json with this setting.

https://developers.google.com/maps/documentation/javascript/using-typescript

{
  "compilerOptions": {
    "types": ["google.maps"]
  }
}

in the root of your project you have to create ts.config.json and add that property single property. this is how you create ts.config.json:

   npx tsc --init
Sign up to request clarification or add additional context in comments.

2 Comments

This answer is what tipped me off the most to solving this issue for myself. According to Google's Map docs it says to install using npm i -D @types/google.maps I took @Yilmaz 's suggestion of adding a "types" property to my tsconfig file, but instead of googlemaps I used google.maps.. and suddenly my Typescript is picking up all of Google Maps' typings from DefinitelyTyped.
@EarlePoole i think I made a typo :) thanks
3

I thought about this some more. If you have not installed the Google Map typings from DefinitelyTyped, I suggest doing so:

npm install --save @types/googlemaps. (Or --save-dev if you only want them for your own development.)

2 Comments

I think you mean `'npm' not 'nom'
Corrected. Thank you. Autocorrect often does more damage than good, but my mistake in not catching the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.