7

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?

  • 1
    can you please provide definition of device.onlineState, typeof(device). – Parv Sharma Mar 22 '18 at 15:59
  • sometimes after there is a compilation error in the Ts code. The javascript generator does not run. This causes anything dependent on the generated js to use the last generated js. Which in your case will contain device.onlineState – Parv Sharma Mar 22 '18 at 16:00
  • @ParvSharma It's a device object and one of the properties is the onlineState which returns Online or Offline, if thats what you mean? – idontunderstandarrays Mar 22 '18 at 16:06
  • This peeves me so dang much. JavaScript/Typescript, the brackets typically do NOT start on new lines. I know it really doesn't matter, but seeing JavaScript/Typescript formatted like this hurts my brain -- and actually makes it harder for me to read your code and help. – Pytth Mar 22 '18 at 16:08
  • @Pytth I've edited it. Is this clearer to you? – idontunderstandarrays Mar 22 '18 at 16:14
  • @idontunderstandarrays YES! Thank you! – Pytth Mar 22 '18 at 17:10

1 Answers1

15

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

let onlineDevicesArray:number[] = [];
let offlineDevicesArray:any[] = [];
Vayrex
  • 1,417
  • 1
  • 11
  • 13
  • I've fixed it now, used a forEach instead of the second for loop. Thanks for your help anyways! – idontunderstandarrays Mar 27 '18 at 08:22
  • You may have never type in group.devices declaration. group.devices = []; But as code gor Group is not shown, it is impossible to find place with never type. – Vayrex Mar 27 '18 at 10:06