实现陀螺仪权限请求,但我在 requestPermission 上收到 typescript 错误
我的代码:
if (typeof (DeviceMotionEvent) !== 'undefined' && typeof (DeviceMotionEvent.requestPermission) === 'function') {
return DeviceMotionEvent.requestPermission()
.then((response: string) => response === 'granted');
}
TS2339: Property 'requestPermission' does not exist on type '{ new (type: string, eventInitDict?:
DeviceMotionEventInit): DeviceMotionEvent; prototype: DeviceMotionEvent; }'.
这个有点挣扎。我尝试像这样(DeviceMotionEvent.requestPermission() as any)转换请求权限,但它保持不变。因为它不是一个模块,所以我不能只做 yarn add @types/...
最佳答案
您需要转换对象,而不是函数,试试这个:
(DeviceMotionEvent as any).requestPermission()
关于javascript - 使用 Typescript 的 DeviceMotionEvent 请求权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60640018/