Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: Property does not exist on type 'never', but function works?

Tags:

typescript

This is my code

sumDevices() {
let onlineDevicesArray = [];
let offlineDevicesArray = [];

for(let group of this.groups[this.selectedDeviceSource.id]){
  for(let device of group.devices){
    if(device.onlineState == "Online"){
      onlineDevicesArray.push(device.onlineState);
      this.onlineDevices = onlineDevicesArray.length;}
    else{
      offlineDevicesArray.push(device.onlineState);
      this.offlineDevices = offlineDevicesArray.length;
    }
  }
}

}

It gives an error on device.onlineState, but the function does work.

TS2339:Property 'onlineState' does not exist on type 'never'.

Can anyone explain to me why its giving me this error?

like image 315
idontunderstandarrays Avatar asked Mar 22 '18 15:03

idontunderstandarrays


1 Answers

You need to provide type for arrays, by default it is "never". Something like this should work.

let onlineDevicesArray:number[] = [];
let offlineDevicesArray:any[] = [];
like image 157
Vayrex Avatar answered Oct 31 '22 22:10

Vayrex