Why is getVolume typed as number | VolumeResult?
puckey opened this issue · comments
/**
* Get the current device volume.
* @param {AndroidVolumeTypes} type - The type of volume you want to retrieve. Defaults to 'music' (Android only, no-op on iOS).
* @returns {Promise<VolumeResult>} - Returns a promise that resolves to an object with the volume value.
*
* @example
* ```ts
* const { volume } = await VolumeManager.getVolume('music'); // type is no-op on iOS
* ```
*/
export async function getVolume(
type: AndroidVolumeTypes = 'music'
): Promise<VolumeResult | number> {
return await VolumeManagerNativeModule.getVolume(type);
}
With the return type of getVolume()
being Promise<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?
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 }
, wherevolume
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!