2

I have defined an enum like this:

enum VideoCategoryEnum {
  knowledge = 0,
  condition = 1,
  interview = 2,
  speech = 3,
  entertainment = 4,
  news = 5,
  advertisement = 6,
  others = 7,
}

I want to set a common function to all the enum so that I can do:

VideoCategoryEnum.tostring() // 'VideoCategoryEnum'

Is it possible to do this? thanks.

AirNoir
  • 251
  • 1
  • 2
  • 15
  • You want to stringify the name of the enum with a custom `.tostring()` method? Or am I missing something here? – Abana Clara Aug 22 '19 at 03:35
  • 1
    See: https://stackoverflow.com/questions/29191451/get-name-of-variable-in-typescript...but this appears to be an X/Y problem. *Why* do you want to do this? – Blue Aug 22 '19 at 03:39
  • @AbanaClara or maybe provide some functions like "getName" and "setName". – AirNoir Aug 22 '19 at 03:40
  • @AirNoir why would you want to modify the name of the enum programmatically? – Abana Clara Aug 22 '19 at 03:41
  • @AbanaClara No, I don't want to modify name. I am just wondering if I could give a name to enum. – AirNoir Aug 22 '19 at 03:43
  • @FrankerZ it's useful to me. I am trying to write a common function to loop the enum props with i18n translated. I don't want to pass a second parameter like getEnumList(enumObject, 'enumeName'). – AirNoir Aug 22 '19 at 03:46
  • @FrankerZ I just tried the method provided by the link. It could only get the variable name, but not the enum's name. for ex. getEnumList(enumObject) , the method will return 'enumObject' instead of 'VideoCategoryEnum' . – AirNoir Aug 22 '19 at 03:59

2 Answers2

2

Typescript is basically just a compile time type checking built on top of Javascript. Typescript enum types are just map (object) under the hood, you can have a look at this answer

Therefore, implement a toString function to an enum type is quite straight forward. You can do it after defining the enum type. Just make sure that you don't put your implementation on .d.ts files because these won't be compile into javascript code.

According the the answer link above, your enum type would be compiled into:

var VideoCategoryEnum;
(function (VideoCategoryEnum) {
   VideoCategoryEnum[VideoCategoryEnum["knowledge"] = 0] = "knowledge";
   VideoCategoryEnum[VideoCategoryEnum["condition"] = 1] = "condition";
   // ...
})(VideoCategoryEnum || (VideoCategoryEnum = {}));
;

/* would equivalent to the following:
   VideoCategoryEnum = {
      "knowledge": 0,
      "condition": 1,
      ...
      "0": "knowledge",
      "1": "condition",
   }
*/
// since it's just basically an object, you can do whatever you want with it like below
// your implementation of toString
VideoCategoryEnum.toString = function() { return 'VideoCategoryEnum'; };
Dat Pham
  • 1,765
  • 15
  • 13
  • It resolves the problem, but makes the declaration of enum more complicated also. Anyway, thanks for your help :) – AirNoir Aug 22 '19 at 04:19
1

ts-nameof should do what you are looking for.

There's a couple of options if you're using babel, and one for tsc.

In your above example, it is simply used as such:

enum VideoCategoryEnum {
  knowledge = 0,
  condition = 1,
  interview = 2,
  speech = 3,
  entertainment = 4,
  news = 5,
  advertisement = 6,
  others = 7,
}

nameof(VideoCategoryEnum); // VideoCategoryEnum
jaws
  • 1,952
  • 4
  • 20
  • 27