JavaScript Question

Hi,

can someone explain me. why this is working:

initMap(map) {

this.getPosition().then((position) =>
{

this.drawMap(position, map);

});
}

drawMap(position, map) {

let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

let mapOptions = {
center: latLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

this.map = new google.maps.Map(map, mapOptions);

}

And this is giving me “Property ‘coords’ does not exist on type ‘{}’.”:

this.getPosition().then((position) =>
{
let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

  let mapOptions = {
    center: latLng,
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  this.map = new google.maps.Map(map, mapOptions);
}

);

I thought this is just the same ? Why is this not working ?

Thank you

I would look at what position’s value looks like in the second sample to debug this issue.

I am a fan of TypeScript’s noImplicitAny flag. If you set it in tsconfig.json by adding this to the compilerOptions stanza:

"noImplicitAny": true,

You will probably see what is going on. I suspect in the first case, the position argument to drawMap is implicitly any, which is papering over what is probably the root cause: the return value of getPosition isn’t properly typed so that the compiler knows to expect it to have a coords property.

Hi rapropos,

your guess was right. With “noImplicitAny”: true i get this “Variable ‘google’ implicitly has an ‘any’ type” - message.

Thank you :slight_smile:

Im just wondering, what i have to do, to fix this “problem”. “google” is coming from the Google Maps Javascript API, how can i tell the compiler, that this “google” is from there while compiling ?

Thank you!

I would start with npm install @types/googlemaps.