Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
How to use enum-like javascript-class that assigns its values without constructor

I have this class, which is supposed to represent an enum:

export default class ChangeStatusEnum {
    
        "added" = "added";

        "deleted" = "deleted";

        "edited" = "edited";

        "unedited" = "unedited";

    static constructFromObject(object) {
        return object;
    }
}

It is generated in the pipeline by openapi-generator1 so changing it is not an option. This is not a question on best practices for defining enums in vanilla-js or typescript, this question is about how to use this class.

I do not understand the syntax of assigning to a string, I do not know where these four strings are accessible.

Here are a few things that I have tried (in a jenkins-test so they can be run easily):

test("access", () => {
    console.log(ChangeStatusEnum) // prints [class ChangeStatusEnum]
    console.log(JSON.stringify(ChangeStatusEnum)) // prints undefined
    console.log(
        ChangeStatusEnum.constructFromObject("deleted") === "deleted"
    ) // prints true
    console.log(
        ChangeStatusEnum.constructFromObject("nonexisting") === "nonexisting"
    ) // also prints true, which means this syntax has no value over just using strings instead of enums
    console.log(ChangeStatusEnum["added"]) // prints undefined
    console.log(ChangeStatusEnum.added) // prints undefined
})

The least I expect from a datastructure that calls itself "enum" is that I can construct and compare values of it without fear of silently constructing non-existing values. Iterating over all values of an enum would also be nice, but is not strictly necessary. I suppose there is a way to do that with this datastructure that I just do not know of due to lack of knowledge of advanced javascript-constructs.

1 The tool is openapi-generator-cli in version 2.5.1 https://www.npmjs.com/package/@openapitools/openapi-generator-cli with openapi-generator-maven-plugin version 6.0.0 https://mvnrepository.com/artifact/org.openapitools/openapi-generator-maven-plugin Since this is somewhat mature tooling I expect their enum-solution to be usable, this is why I ask this question as a js-question and not as an openapi-question.

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!