Search code examples
javascripttypescriptbatterymanager

navigator.getBattery() does not exist on type Navigator


I'm trying a custom React hook called useBattery to access the battery status but TypeScript says that getBattery() does not exist on type Navigator:

enter image description here

Based on the MDN documentation the interface name is BatteryManager but I did not find this interface.


Solution

  • Because this is not standardized, and also not supported by all browsers, the Typescript team decided that it should not be implemented.

    For background see here: https://github.com/microsoft/TypeScript/issues/15314

    You can probably get around this by casting navigator to any and then calling the required functions, but you'll have to make it robust in case the browser does not support it.