react-native-geolocation-service
react-native-geolocation-service copied to clipboard
Using typescript with Geolocation.getCurrentPosition promise wrapping
Hi, I tried to wrap Geolocation.getCurrentPosition to promise to have possibility use it with async await. It looks like this: const getCurrentPosition = (): Promise<GeolocationPosition> => new Promise((resolve, reject) => Geolocation.getCurrentPosition(resolve, reject, opt), );
but I have typescript error : TS2345: Argument of type '(value: GeolocationPosition | PromiseLike<GeolocationPosition>) => void' is not assignable to parameter of type 'SuccessCallback'. Types of parameters 'value' and 'position' are incompatible. Type 'GeoPosition' is not assignable to type 'GeolocationPosition | PromiseLike<GeolocationPosition>'. Type 'GeoPosition' is not assignable to type 'GeolocationPosition'. The types of 'coords.altitudeAccuracy' are incompatible between these types. Type 'number | null | undefined' is not assignable to type 'number | null'. Type 'undefined' is not assignable to type 'number | null'.
Do someone know which type should I use in this case? Thanks!
Hello, have you tried to type your Promise ?
return new Promise<Geolocation.GeoPosition>((resolve, reject) => { Geolocation.getCurrentPosition( resolve, reject, { ...your_options }, ) })
Hello, have you tried to type your Promise ?
return new Promise<Geolocation.GeoPosition>((resolve, reject) => { Geolocation.getCurrentPosition( resolve, reject, { ...your_options }, ) })
Thanks for the help! You also resolved an issue I had where the type wasn't the expected GeolocationPosition
but was instead Geolocation.GeoPosition
. I couldn't figure that out for a long while, so thank you!
Closing as it's already answered.