Agontuk / react-native-geolocation-service

React native geolocation service for iOS and android
https://www.npmjs.com/package/react-native-geolocation-service
MIT License
1.61k stars 291 forks source link

Using typescript with Geolocation.getCurrentPosition promise wrapping #332

Closed nikita-usichenko closed 2 years ago

nikita-usichenko commented 2 years ago

Hi, I tried to wrap Geolocation.getCurrentPosition to promise to have possibility use it with async await. It looks like this: const getCurrentPosition = (): Promise => new Promise((resolve, reject) => Geolocation.getCurrentPosition(resolve, reject, opt), );

but I have typescript error : TS2345: Argument of type '(value: GeolocationPosition | PromiseLike) => 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'.       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!

thomasviaud commented 2 years ago

Hello, have you tried to type your Promise ?

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

MendelBak commented 2 years ago

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!

Agontuk commented 2 years ago

Closing as it's already answered.