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

178
Views
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
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!