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?