Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Error: Cannot find namespace 'google'

I have project angular-cli

~root~/src/typings.json

{
  "globalDevDependencies": {
    "angular-protractor": "registry:dt/angular-protractor#1.5.0+20160425143459",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "selenium-webdriver": "registry:dt/selenium-webdriver#2.44.0+20160317120654"
  },
  "globalDependencies": {
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160602141504",
    "google.maps": "registry:dt/google.maps#3.20.0+20160914131659"
  }
}

~root~/typings/index.d.ts

/// <reference path="globals/angular-protractor/index.d.ts" />
/// <reference path="globals/es6-shim/index.d.ts" />
/// <reference path="globals/google.maps/index.d.ts" />
/// <reference path="globals/hammerjs/index.d.ts" />
/// <reference path="globals/jasmine/index.d.ts" />
/// <reference path="globals/selenium-webdriver/index.d.ts" />

~root~/src/tsconfig.json

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "mapRoot": "./",
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types",
      "../typings"
    ],
    "files": [
      "../typings/index.d.ts"
    ]
  }
}

After run ng serve I have error message in console

ERROR in [default] F:~root~\src\app\ui\google-map\map-marker\map-marker.directive.ts:7:26

Cannot find namespace 'google'

and

ERROR in [default] ~root~\src\app\trip-entry-page\trip-entry-page.component.ts:188:21

Cannot find name 'google'

~root~\src\app\ui\google-map\map-marker\map-marker.directive.ts:7:26

... 
@Input() veyoMapMarker: google.maps.MarkerOptions 
...

~root~\src\app\trip-entry-page\trip-entry-page.component.ts:188:21

... 
if (status === google.maps.DirectionsStatus.OK) { 
...

After build app correct working

How me resolve this Error messages?

like image 412
mego4iter Avatar asked Oct 07 '16 04:10

mego4iter


4 Answers

A bit of a late response but I had a similar issue using Angular CLI RC.0.

It turned out that I hadn't install and imported the typings, which can be done as follows:

npm install --save-dev @types/googlemaps

import {} from '@types/googlemaps';
like image 101
Stephen Paul Avatar answered Nov 10 '22 23:11

Stephen Paul


In IONIC 4 I fixed it by installing @types/google-maps not @types/googlemaps

just run this

npm install @types/google-maps --save

and import google in your component using

import { google } from "google-maps";
like image 30
Mab Kiani Avatar answered Nov 11 '22 01:11

Mab Kiani


I was facing same problem and what I did was, I just removed these

import { } from 'googlemaps';
declare var google: any;

from my component.ts and add "types": [ "googlemaps" ] in my tsconfig.app.json . . Now my tsconfig.app.json looks like this.

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "outDir": "../out-tsc/app",
        "module": "es2015",
        "types": [
            "googlemaps"
          ]
    },
    "exclude": [
        "test.ts",
        "**/*.spec.ts"
    ]
}

And its working perfectly.

like image 7
MaBbKhawaja Avatar answered Nov 10 '22 23:11

MaBbKhawaja


For Angular 9.****

I tried installing @types/googlemaps, It did not work for me.

I downgrade it to "@types/googlemaps": "3.39.12" then worked perfectly fine.

this is my code

In tsconfig.app.json (add googlemaps to types array)

"types": [
      "googlemaps"
    ]

In app module.ts

import { AgmCoreModule } from '@agm/core';
.
.
.
.
 imports: [
    BrowserModule,
    AppRoutingModule,
    AgmCoreModule.forRoot({
      apiKey: '...KEY...',
      libraries: ['places']
    })
  ],

To downgrade @types/googlemaps, go to pakage.json file in the project root folder and change @types/googlemaps version to "3.39.12". it's better if you delete files form

node_module -> @types -> googlemaps

Then in the terminal enter

npm install
like image 2
gamal Avatar answered Nov 11 '22 01:11

gamal