react-native-geolocation-service icon indicating copy to clipboard operation
react-native-geolocation-service copied to clipboard

Using typescript with Geolocation.getCurrentPosition promise wrapping

Open nikita-usichenko opened this issue 3 years ago • 2 comments

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!

nikita-usichenko avatar Jan 26 '22 23:01 nikita-usichenko

Hello, have you tried to type your Promise ?

return new Promise<Geolocation.GeoPosition>((resolve, reject) => { Geolocation.getCurrentPosition( resolve, reject, { ...your_options }, ) })

thomasviaud avatar Feb 01 '22 11:02 thomasviaud

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!

MendelBak avatar Jul 07 '22 13:07 MendelBak

Closing as it's already answered.

Agontuk avatar Sep 03 '22 06:09 Agontuk