42

I'm using typescript 2.0 with the lastest ionic@RC.0 build process.

I installed the google-maps types like this:

npm install @types/google-maps --save-dev --save-exact

and I'm trying to import some of the type definitions into my code like this

/// <reference types="google-maps" />
import { LatLng, LatLngBounds } from 'google-maps';

but I get this typescript error:

./node_modules/@types/google-maps/index.d.ts has no exported member 'LatLng'

and if I look in the source, I actually find the definition in

./node_modules/@types/google-maps/node_modules/@types/googlemaps/index.d.ts

michael
  • 4,377
  • 8
  • 47
  • 73

3 Answers3

17

Add a reference to the types package in the file tsconfig.json in the root folder of your project:

"types": [
    "google-maps"
]

Don't import anything in your source files, the types are globally defined.

Ropez
  • 3,485
  • 3
  • 28
  • 30
  • 2
    The problem is that you will have to specificy ALL types like this if you start adding some manually, as far as I understand TypeScript won't load them automatically. This feature is firest meant as whitelist, not a global import system. – Eric Burel Mar 25 '20 at 06:34
  • 1
    Docs: https://www.typescriptlang.org/tsconfig#types – Bin Ury Jul 27 '21 at 19:26
  • https://www.typescriptlang.org/docs/handbook/modules.html#importing-types says `import type { APIResponseType } from "./api";` – Ryan Oct 15 '21 at 17:14
2

The import only works for the current package, not its dependencies.

So you need to import { LatLng, LatLngBounds } from 'googlemaps'

jifeng.yin
  • 2,081
  • 1
  • 21
  • 19
  • 4
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/18095085) – Rajesh Ujade Nov 29 '17 at 09:08
  • 1
    Actually, my answer is the same as @Sam. Please take carefully it's `googlemaps`! – jifeng.yin Sep 03 '18 at 02:51
2

You are checking the wrong declaration file. The one you are using is: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/google-maps/index.d.ts which does not expose LatLng.

The declaration file you linked to is: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/googlemaps/index.d.ts

googlemaps vs google-maps

Sam
  • 14,642
  • 6
  • 27
  • 39