Closed puckey closed 10 months ago
Thanks for pointing this out. You're correct that the current typing of the getVolume()
function can be confusing. If the function returns a number
, destructuring volume
as in the example would indeed fail.
There are two options moving forward:
I could modify the function to always return a VolumeResult
. This means, in some cases, you would receive an object like { volume: number }
, where volume
represents the type of volume you requested.
Alternatively, it might be safer to check the type of the returned value before trying to destructure it, like so:
const result = await VolumeManager.getVolume('music');
if (typeof result === 'number') {
console.log('The volume is ' + result);
} else {
console.log('The music volume is ' + result.music);
}
I'd appreciate your thoughts on which of these approaches you'd prefer. Alternatively, if you have other suggestions, I'd be open to considering them.
Fixed in https://github.com/hirbod/react-native-volume-manager/releases/tag/v1.6.0 This release is a breaking change.
I've decided to always return VolumeResult (result.volume) and also include all android specific volumes as well.
@hirbod good choice - thanks for the new release!
With the return type of
getVolume()
beingPromise<VolumeResult | number>
, wouldn't the example code fail:const { volume } = await VolumeManager.getVolume('music');
Perhaps the
number
type may be removed as a return type?