Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property "usb" does not exist on type "Navigator" in angular2 typescript project

I am using below code just to see devices connected on usb port. Getting error "Property usb does not exist on type Navigator" at compile time when I run 'ng serve' command in command prompt.

ngOnInit() {
    async () => {
        let devices = await navigator.usb.getDevices();
        devices.forEach(device => {
          // Add |device| to the UI.
          console.log(device);
        });
    }
}
like image 490
ttmkk Avatar asked Oct 20 '25 21:10

ttmkk


2 Answers

yarn add -D @types/w3c-web-usb

or

npm install --save-dev @types/w3c-web-usb

Then, add the following directive at the top of your typescript file:

/// <reference types="w3c-web-usb" />
like image 102
Réda Housni Alaoui Avatar answered Oct 22 '25 12:10

Réda Housni Alaoui


The best solution is to use @types/w3c-web-usb typing package.

Add it to your project using either yarn:

yarn add --dev @types/w3c-web-usb

or npm

npm install --save-dev @types/w3c-web-usb

like image 34
kulak Avatar answered Oct 22 '25 11:10

kulak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!