18

We have a situation at the moment with our code where we are using Enums in our Java layer which store an id and a 'display value' with a constructor like below:

public enum Status implements EnumIdentity {

    Active(1, "Active"),
    AwaitingReview(2, "Awaiting Review"),
    Closed(3, "Closed"),
    Complete(4, "Complete"),
    Draft(5, "Draft"),
    InProcess(6, "In Process"),
    InReview(7, "In Review"),
    NotStarted(8, "Not Started"),
    PendingResolution(9, "Pending Resolution"),
    Rejected(10, "Rejected");

    private int id;
    private String displayValue;

    PlanStatus(final int id, String displayValue) {
        this.id = id;
        this.displayValue = displayValue;
    }

    /** {@inheritDoc} */
    @Override
    public int id() {
        return id;
    }

    public String getDisplayValue() {
        return displayValue;
    }
}

and we would like something in typescript to match this to allow for displaying the status in a meaningful way for carrying out logic and for display the value to the user on the front end. Is this possible? Is there a better way to handle this? We would like to avoid having to use logic such as does status.id() = 1 or status.name() = 'Active' hence for the push towards enums.

Thanks

2

1 Answer 1

33

Typescript does not support expanded enums such as in java. You can achieve a similar effect using a class:

interface EnumIdentity { }
class Status implements EnumIdentity {

    private static AllValues: { [name: string] : Status } = {};

    static readonly Active = new Status(1, "Active");
    static readonly AwaitingReview = new Status(2, "Awaiting Review");
    static readonly Closed = new Status(3, "Closed");
    static readonly Complete = new Status(4, "Complete");
    static readonly Draft = new Status(5, "Draft");
    static readonly InProcess = new Status(6, "In Process");
    static readonly InReview = new Status(7, "In Review");
    static readonly NotStarted = new Status(8, "Not Started");
    static readonly PendingResolution = new Status(9, "Pending Resolution");
    static readonly Rejected = new Status(10, "Rejected");

    private constructor(public readonly id: number, public readonly displayValue: string) {
        Status.AllValues[displayValue] = this;
    }

    public static parseEnum(data: string) : Status{
        return Status.AllValues[data];
    }

}
Sign up to request clarification or add additional context in comments.

8 Comments

That's a shame, I was coming to that conclusion but thought it best to post out to the community in case someone found a similar way of working around this. Thanks
How will a json string like this {"id":1, "title":"Test","status":"AwaitingReview"}] map to a class when the status gets represented as a string?
That will be a manual affair I'm afraid. You will need to use the parseEnum method o added to convert to the enum ..
I would never have thought how to do this, how is the AllValues defined, spitting out an error on me?
Thanks for the answer - I am newer to TS, but have many years of Java under my belt and I asked my team this question in our last session together. I'm not sure if anyone knew exactly what I was asking because of differences in abilities, but this answered it perfectly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.