0

So, I have 2 files: (1) conveyor-status.ts (2) overview.component.ts. There is a statement inside overview.component.ts which goes: public possibleStatusses: string[] = Object.keys(ConveyorStatus);.

Now, here's the tricky part. When the contents of conveyor-status.ts is:

export enum ConveyorStatus {
    Stopped = "Stopped",
    Estop = "Estop",
    DisconnectFault = "DisconnectFault",
    Manual = "Manual",
    EncoderFault = "EncoderFault",
    Jam = "Jam",
    Full = "Full",
    Fault = "Fault",
    EnergyManagement = "EnergyManagement",
    CascadeStop = "CascadeStop",
    Running = "Running",
  }

the value of possibleStatusses is: ["Stopped", "Estop", "DisconnectFault", "Manual", "EncoderFault", "Jam", "Full", "Fault", "EnergyManagement", "CascadeStop", "Running"] which is desired.

Unfortunately, I cannot change the contents of conveyor-status.ts (due to a large number of other files depending upon the established structure), and hence, that file MUST be as below:

export enum ConveyorStatus {
    Stopped, // default 0
    Estop, // 1
    DisconnectFault, // 2
    Manual, // 3
    EncoderFault, // 4
    Jam, // 5
    Full, // 6
    Fault, // 7
    EnergyManagement, // 8
    CascadeStop, // 9
    Running, // 10
}

As soon as I switch it to what is MUST be, the value of possibleStatusses changes to: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Stopped", "Estop", "DisconnectFault", "Manual", "EncoderFault", "Jam", "Full", "Fault", "EnergyManagement", "CascadeStop", "Running"].

I am completely unable to understand why is this happening. Can someone please help me with this?

CuriousLearner
  • 361
  • 1
  • 6
  • 14
  • 1
    Check this answer: https://stackoverflow.com/questions/49124655/enum-type-gives-double-array-in-typescript You can see your enum compiled to JS here: https://www.typescriptlang.org/play?#code/KYOwrgtgBAwg9iAbsAnnATgZQC4ENtgDOUA3gFBSVQ5wAOtwAJgDRQD0bUjwAZrmABtsUAAwUqAUULY6rDlACM4ygBEAloQDGCEME3YAYvyFzOAJmVQAsrnC4BpqAGZLEkNu7ojg7I4AslgBSuBCOAKyWBoIO7JwAbJHGvrFQAOyuuugA5ig2ILhZwBCgyfIAHJYwuFq43DS0jgCclgBKYCAgaiBZjgpiAL5kQA – Velja Radenkovic Apr 26 '21 at 22:33
  • Although, I don't think you can take every other element. Not knowing better at this moment, I would use Object.values instead of object keys and take the first half of the array as a result. I am curious about the elegant way of doing this. – Velja Radenkovic Apr 26 '21 at 22:43
  • @VeljaRadenkovic Thanks for your input. Looks like the most "elegant" way for now would be to either use the first half of the array using Object.values() or second half using Object.keys(). It's a little weird that it works that way. – CuriousLearner Apr 27 '21 at 20:28

0 Answers0