Issue
EDIT
As comment suggests, Enum is not part of JavaScript but part of TypeScript. I intentionally left original title as one may make mistake like me.
I have two enums with the same keys but different values.
enum RowStates {
editing = 0,
sentToApproval,
approved
// ...
}
enum RowColors {
editing = '#ffffff',
sentToApproval = '#ffffcc',
approved = '#ccffb3'
// ...
}
And I have some function to do convertion:
function Convert (rowState) {
// What should be here to return rowColor?
// Using switch (rowState) is obvious, but may be other solution exist?
}
Solution
TypeScript enums allow you to do a reverse mapping:
enum RowStates {
editing = 0,
sentToApproval,
approved
}
enum RowColors {
editing = '#ffffff',
sentToApproval = '#ffffcc',
approved = '#ccffb3'
}
function convert(rowState: RowStates) {
return RowColors[RowStates[rowState] as keyof typeof RowColors];
}
console.log(convert(RowStates.sentToApproval)); // prints '#ffffcc'
Answered By - Robby Cornelissen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.