简体   繁体   中英

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

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?

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

let onlineDevicesArray:number[] = [];
let offlineDevicesArray:any[] = [];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM