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

169
Visualizações
Set Typescript class member's value based on another member of the same class

Let's say I have a class User:

export class User {
  constructor(
    userID?: number,
    state?: 'In' | 'Out' | 'Break' | 'Lunch', // for example, but there are more...
    stateVal?: 1 | 2 | 3 | 4,
  ) {}
}

The purpose of stateVal is to make sorting by state to be in a specific order and not alphabetical, i.e. by default it sorts by alphabetical which can result in undesired order->

(default, alphabetical)
(1) Break
(2) In
(3) Lunch
(4) Out

(desired order)
(1) In
(2) Break
(3) Lunch
(4) Out

That way, if I sort this stateVal rather than state, I can get the order I want.

Overall, what happens is, I get a list of users from a RESTful API, then I must add in a stateVal by looping through each User and adding stateVal manually (since stateVal is not provided by the server).

BUT can this be done at the class level?

I'm wondering:

Is it possible to automatically map the state property to trigger stateVal to be a value depending on itself?

i.e.

If state is 'In', automatically set stateVal to 1 -- from a Class point of view? Somehow enforce this logic?

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