angulartypescriptw3c-geolocation

How to use GeolocationPosition correctly in TypeScript?


We want to access the user's location using the Geolocation API.

This snippet works fine:

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition((position: GeolocationPosition) => console.log(position));
}

VSCode also doesn't show any errors or warnings, but the Angular CLI throws this error:

Cannot find name 'GeolocationPosition'.
navigator.geolocation.getCurrentPosition((position: GeolocationPosition) => console.log(position));

The project is using the following things:

Also strict type checking is enabled in Angular CLI

How to use this type and other types from that API correctly?


Solution

  • Running TypeScript in v4.0.x brings up Position as the typename, but that appears to have been renamed to GeolocationPosition in 4.1.x. Can't find any docs on why they did that. See: https://stackoverflow.com/a/66005573/510048.

    Recommend updating to the latest version of Typescript (currently 4.2.3)