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

91
Visualizações
Is there a concise way to write a set of enums as strings?

When i write enums in javascript I often write:

const movementState = {
  stopped: 'stopped',
  walking: 'walking',
  running: 'running',
};

This enables me to use it like movementState.stopped and linting ensures it exists, also if I log it, it's value is human readable.

I'd like something like:

enum movementState = {stopped, walking, running};

Note: I have no interest in doing this in typescript. I do my survival work-job in TS, I do my passion in JS.

I'm not super up to date w the standard... so I was hoping maybe something new was added in one of the stages or something. Thanks~

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

One option would be to map an array of the strings to an array of entries, using the string as the value and as the key, then turn it into an object with Object.fromEntries. Not extraordinarily concise, but I'm not sure if you can get anything better.

const makeEnum = strs => Object.fromEntries(
  strs.map(str => [str, str])
);

const movementState = makeEnum(['stopped', 'walking', 'running']);
console.log(movementState.running);

about 4 years ago · Juan Pablo Isaza Relatório
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