Error in Ionic2 TypeScript: 'google' and '$' names not found, despite Google Maps and jQuery functioning correctly

I am currently working on developing an ionic2 application using TypeScript.

Within the index.html file, I have attempted to integrate jquery and the Google Maps JS API before cordova.js:

  <!-- Vendor -->
  <script src="https://maps.googleapis.com/maps/api/js?key=XXXXX&signed_in=false"></script>
  <script src="asset/jquery.min.js"></script>

  <!-- cordova.js required for cordova apps -->
  <script src="cordova.js"></script>

The inclusion of Google Maps and jquery within the app is functioning properly, but I am puzzled by these error messages that appear when running the "ionic serve" command:

TypeScript error: /Users/xxx/client_app/app/pages/step1/step1.ts(15,21): Error TS2304: Cannot find name 'google'.
TypeScript error: /Users/xxx/client_app/app/pages/step1/step1.ts(30,18): Error TS2304: Cannot find name 'google'.
TypeScript error: /Users/xxx/client_app/app/pages/step1/step1.ts(37,24): Error TS2304: Cannot find name 'google'.
TypeScript error: /Users/xxx/client_app/app/pages/step1/step1.ts(39,5): Error TS2304: Cannot find name '$'.
TypeScript error: /Users/xxx/client_app/app/pages/step1/step1.ts(39,42): Error TS2304: Cannot find name '$'.
TypeScript error: /Users/xxx/client_app/app/pages/step1/step1.ts(39,76): Error TS2304: Cannot find name '$'.

Below is a segment of my code from step1.ts:

  ngOnInit() {
    this.gmap = new google.maps.Map(document.getElementById('gmap_canvas'), {
            center: {
                lat: 22.280102,
                lng: 114.183751
            },
            zoom: 16,
            streetViewControl: false,
            styles : [{
                featureType: "poi",
                elementType: "labels",
                stylers: [
                    { visibility: "off" }
                ]
            }],
            mapTypeControlOptions: {
                mapTypeIds: [google.maps.MapTypeId.ROADMAP]
            },
            overviewMapControl: false,
            mapTypeControl: false,
        });

    this.gmap.controls[google.maps.ControlPosition.TOP_CENTER].push(document.getElementById('gmep_center_marker'));

    $('#gmep_center_marker').css({ top: ($('#gmap_canvas').height() / 2) - $('#gmep_center_marker').height() });
  }

Answer №1

To solve this issue easily, make sure to include variable declarations at the top of your file:

import {...} from '...';

declare var google: any;
declare var $: any;

Alternatively, I recommend installing the appropriate type definition files. For JQuery, you can do so by running the following command:

typings install jquery --ambient --save

Once installed, add the following line to the top of your component file:

///<reference path="../typings/jquery/jquery.d.ts"/>

import {...} from '...';

If there isn't a typings file available for the Google API, you can use the quick fix mentioned above specifically for utilizing Google functionalities.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to optimize reactive values using the Vue composition API?

Imagine I have a block of code like this... const computedStyle = computed(() => normalizeStyle([undefined, styleProp, undefined]) ); const computedClass = computed(() => normalizeClass([ "button", classProp, { "b ...

Issue with Ionic and Angular: Struggling to empty input field

Within my Ionic app, there is a form that contains an input field and a button. Upon clicking the button, an action should occur within the controller to clear the input field. Unfortunately, this functionality is not working as expected. Despite its simpl ...

Angular 2 offers a powerful feature called ngFor that allows developers to

Is there a way to allow users to enter keywords in an input field to filter items within a list of menu items dynamically without using ngModel? I need this to be done without the use of buttons as well. Any suggestions for a workaround? <div class=" ...

Exploring Angular 2 Tabs: Navigating Through Child Components

Recently, I've been experimenting with trying to access the HTML elements within tabs components using an example from the Angular 2 docs. You can view the example here. Here is a snippet of my implementation: import {Component, ElementRef, Inj ...

Angular 13: Issue with displaying lazy loaded module containing multiple outlets in a component

Angular version ^13.3.9 Challenge Encountering an issue when utilizing multiple outlets and attempting to render them in a lazy module with the Angular router. The routes are being mapped correctly, but the outlet itself is not being displayed. Sequence ...

How can you test component methods in Angular?

As a beginner in Angular, I am currently learning how to write tests and struggling with mocking and testing methods from components. The structure of my HTML is as follows: There is a table displaying all certificates. By clicking the "edit" button, you ...

Angular implementing a carousel feature using the ngFor directive

Currently, I am working on implementing a carousel feature. The images in the carousel are sourced from a string array and I want to be able to cycle through them when clicking on the left or right arrows. How can I achieve this functionality using only ...

I am trying to access a value saved in a service in Angular 8 component and use it in other services. Can anyone help

Setting a value through a component export class UniqueComponent { constructor(service:UniqueService){ } count =0 ; onRefresh(){ this.service.count = 1; } } Using the value in the service UniqueService{ count:any; doSomething(){ //using count ...

Retrieve a HashMap through an HTTP GET request using Angular 10

I am currently using the following versions: Angular CLI: 10.0.1 Node: 12.18.2 OS: win32 x64 Angular: 10.0.2 In my setup, I have a Java Spring Boot service that is functioning correctly and returns data as a HashMap. Map<String, List<String>&g ...

Two tags attached to four hypertext links

Within my HTML code, I have hyperlinks present on every line. However, I am seeking to eliminate these hyperlinks specifically from "your previous balance" and "your new balance".https://i.sstatic.net/ekVGT.png In the following HTML snippet: <tr *ngFor ...

The confirm alert from Material UI is being obscured by the dialog

How can I ensure that a material ui dialog does not hide the alert behind it when confirming an action? Is there a way to adjust the z index of the alert so that it appears in front of the dialog? import Dialog from "@material-ui/core/Dialog"; i ...

Creating a dynamic selection in Angular without duplicate values

How can I prevent repetition of values when creating a dynamic select based on an object fetched from a database? Below is the HTML code: <router-outlet></router-outlet> <hr> <div class="row"> <div class="col-xs-12"> & ...

Verify whether the user is authenticated in Angular 2

For the past couple of days, I've been struggling with figuring out how to determine if a user is authenticated in my Angular-rc1 application and could really use some assistance or guidance. After receiving a token from the server, I want to save it ...

Distinguishing Between TypeScript Interface Function Properties

Could anyone clarify why the assignment to InterfaceA constant is successful while the assignment to InterfaceB constant results in an error? interface InterfaceA { doSomething (data: object): boolean; } interface InterfaceB { doSomething: (data: obje ...

I encountered an error in my Node.js application stating that it could not find the name 'Userdetailshistory' array. I am puzzled as to why this error is occurring and I suspect it may be due to my

import { Component, OnInit } from '@angular/core'; import { UserdetailshistoryService } from '../../services'; @Component({ selector: 'my-userdetailshistory', templateUrl: './userdetails-history.component.html', ...

Ways to display an icon upon hovering or clicking, then conceal it when the mouse is moved away or another list item is clicked using ngFor in Angular 7

Within a simple loop, I have created a list with multiple rows. When a row is clicked or hovered over, it becomes active. Subsequently, clicking on another row will deactivate the previous one and activate the new one. So far, everything is functioning c ...

The TN-Models-FP error message states that it is not allowed to use the `create` model without an associated `entity` model

Utilizing the TN-models-fp library to construct a basic api inspired by the provided examples, here is my implementation in api.ts: import { axios } from '../axios-instance' import { createApi } from '@thinknimble/tn-models-fp' import { ...

How can we ensure file uploads are validated when using class-validator?

Recently, I've been utilizing the wonderful class-validator package to validate data. One specific validation task I'm working on is validating a file upload, ensuring that the file is not empty and ideally confirming that it is an image file. H ...

There was an issue with compiling the template in the module. It was unable to locate the component in relation to the [

I have organized my project folder structure as follows: https://i.sstatic.net/SbZMl.png Inside the cable folder, there is a component path defined as: /folder1/folder2/dialogs/changes-dialog/details-wizard/add-packages/new-package-definition/package-sel ...

Unable to perform navigation during page load in a React.js application

I attempted to navigate to a route that should redirect the user back to the homepage when postOperations isn't set in the localStorage. To save time, please review the code snippet focusing on the useEffect and the first component inside return(). im ...