javascript - 使用 Typescript 的 DeviceMotionEvent 请求权限

标签 javascript reactjs typescript

实现陀螺仪权限请求,但我在 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/

相关文章:

javascript - html 主动打开工具提示(通过代码)

javascript - 通知子组件 Promise 已结束,以便子组件可以执行方法(React Native)

reactjs - VsCode (Ctrl + K + D) - JSX 元素没有相应的结束标记

angular - 无法按照教程使用 angular-in-memory-web-api 0.6.1

javascript - Google Apps 脚本 HTML 表单

javascript - 使用 jQuery 更改单选按钮选择上的标签文本

css - 如何在面板上添加竖线?

reactjs - react native 上下文和弃用的 super 用法

javascript - txtLatLong.split 不是 jquery 中的函数

javascript - 如何将初始状态设置为使用 useQuery 获取的数据?