Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda