WebXR typescript support
Asked Answered
C

2

6

I want to experiment with webxr and have setup a typescript project. According to WebXR

I should do the following:

const supported = await navigator.xr.isSessionSupported('immersive-vr');

With my typescript setup navigator.xr is displayed as error. enter image description here enter image description here

I wonder how to setup typescript for webxr. My tsconfig looks like this:

{
    "compilerOptions": {
        "outDir": "./dist/",
        "noImplicitAny": true,
        "module": "esNext",
        "target": "es6",
        "allowJs": true,
        "moduleResolution": "node"
    },
    "exclude": [
        "./node_modules"
    ],
}
Complexion answered 11/1, 2021 at 8:3 Comment(0)
U
7

You need to add definitions for WebXR, such as these ones. Something like running: npm install --save-dev @types/webxr and then importing the types:

import type { Navigator } from 'webxr';

Related:

Uralian answered 18/1, 2021 at 9:21 Comment(1)
Awesome, I just needed to install the package, the types were added to the global scope.Montespan
B
3

Install typings for webxr with npm i -D @types/webxr in your command line.

Then import XRSystem in your ts file with import type { XRSystem } from 'webxr'; (Take care about the type word in the import).

Then call xr with:

const xr = (navigator as any)?.xr as XRSystem;
xr.requestSession('immersive-vr', {optionalFeatures: ['hand-tracking']})
Bookworm answered 17/7, 2021 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.