Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DeviceMotionEvent Request Permission with Typescript

Implementing a gyroscope permission request, but i get a typescript error on requestPermission

My code:

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; }'.

Struggling a bit with this one. i tried casting request permission like this (DeviceMotionEvent.requestPermission() as any) but it stays the same. Since it's not a module i cannot just do yarn add @types/...

like image 523
Narcil Avatar asked Oct 16 '22 05:10

Narcil


1 Answers

You need to cast the Object, not the function, try this:

(DeviceMotionEvent as any).requestPermission() 
like image 190
Andre Trigo Avatar answered Oct 19 '22 02:10

Andre Trigo