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:
- Angular 11.0.6
- tslib 2.0.0
- ts-node 8.3.0
- tslint 6.1.0
- typescript 4.0.2
Also strict
type checking is enabled in Angular CLI
How to use this type and other types from that API correctly?
navigator.geolocation.getCurrentPosition((position) => console.log(position));
. This way TS will automatically set the type correctly – Silicathis.log(position);
and the method looks like this:log(position: GeolocationPosition): void { console.log(position); }
I still need the type in the function definition. If I simply uselog(position) { }
, the compiler will say the type ofposition
is missing and when I add:any
the type is unknown in the method. Hope you understand my dilemma, @OwenKelvin – Cytaster