Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor on typescript enum?

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

like image 377
Rebecca Douglas Avatar asked Dec 08 '17 11:12

Rebecca Douglas


People also ask

Can I use enum as a type in TypeScript?

Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.

When should I use TypeScript enums?

In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.

Should enums be capitalized TypeScript?

Use uppercase letters for your enums - they are global constants which have usable values. They are more than just types, which use usually like Foo or TFoo . The keys on an enum tend to be titlecase.

Do enums have to be capitalized?

Because they are constants, the names of an enum type's fields are in uppercase letters.


1 Answers

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];
    }

}
like image 170
Titian Cernicova-Dragomir Avatar answered Sep 20 '22 19:09

Titian Cernicova-Dragomir