5

I have a typescript enum that looks like this:

enum State {
    NotRequired,
    Working,
    PendingReview,
    Reviewed,
    Done
}

And this generates this:

var State;
(function (State) {
    State[State["NotRequired"] = 0] = "NotRequired";
    State[State["Working"] = 1] = "Working";
    State[State["PendingReview"] = 2] = "PendingReview";
    State[State["Reviewed"] = 3] = "Reviewed";
    State[State["Done"] = 4] = "Done";
})(State || (State = {}));

I would like to have the values a nicely formed string with spaces where required.

So State[State["PendingReview"] = 2] = "PendingReview"; would become State[State["PendingReview"] = 2] = "Pending Review";

I have managed to achieve something close to this by defining my enum like so:

enum State {
    "Not Required",
    Working,
    "Pending Review",
    Reviewed,
    Done
}

However this has the drawback that to use any enum value in code with a space i now have to use my key instead.

So State.PendingReview now has to be used like this State["Pending Review"]

Can i have the best of both worlds by somehow defining an alternative display string to my key?

So that when i say State[State.PendingReview] it gives me the value "Pending Review"

3 Answers 3

4

As you're not specifying explicit values for the enum, you may as well use a string enum (available as of TypeScript 2.4) for this:

enum State {
  NotRequired = "Not Required",
  Working = "Working",
  PendingReview = "Pending Review",
  Reviewed = "Reviewed",
  Done = "Done"
}

The display value then simply becomes e.g. State.NotRequired.

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

Comments

1

Since enums does not supports string values, would be better to use a class with static fields like this.

You have the compilation checks like if you where using enum, you call it the same way in your code also.

module rizze.tests {


enum State{
     NotRequiered,
     Working,
     Pending,
     Reviewed,
     Done
}

class StateConvert {
    static NotRequiered="Not Required";
    static Working="I'm Working";
    static Pending = "Pending Review";
    static Reviewed="Reviewed Done";
    static Done="Done All";

    static convert(state:State):String{
        if( state == null) return null;
        switch(state){
            case State.NotRequiered:{return StateConvert.NotRequiered;}
            case State.Working:{return StateConvert.Working;}
            case State.Pending:{return StateConvert.Pending;}
            case State.Reviewed:{return StateConvert.Reviewed;}
            case State.Done:{return StateConvert.Done;}

        }
          console.log("Error state  undefined + " +state );
          return null;  

    }
}





 export class StateTest {

      constructor(){
         let state:State = State.Reviewed;
         console.log("state:"+state + " / " + StateConvert.convert(state));   

      }

  }

//TEST
let s:StateTest= new StateTest();

}

2 Comments

I also still want them to be enums as in State.PendingReview == 2 but State[State.PendingReview] == "Pending Review"
you will have to code it manually. Because this is using 2 principles that are completely different and imcompatible (due to typescript langage design)
0

Can i have the best of both worlds by somehow defining an alternative display string to my key?

No. This is not supported in TypeScript. You could try to make a feature request if you think this would be valuable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.