# So trick to reference exported enum?

**URL:** <https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644>\
**Category:** General\
**Created:** [12 December 2023 16:23 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644 "2023-12-12T16:23:49Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [12 December 2023 16:23 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/1 "2023-12-12T16:23:49Z")

</div>

So trick to reference exported enum? How do you reference an exported enum?

```auto
const { LGTV } = require('lgtv-ip-control');
const TV = new LGTV('x.x.x.x', 'xx:xx:xx:xx:xx:xx', XXXXXXXX');

Object.keys(Keys).forEach(key => console.log("Key:", key))

```

The module I am using works... loads, I can execute various functions in the TV, i.e. LGTV class instance (i.e. object). But if I try to reference an exported enum? I think my limited Javascript knowledge is letting me down here?

```auto
Object.keys(Keys).forEach(key => console.log("Key:", key))
            ^
ReferenceError: Keys is not defined
    at Object.<anonymous> (/root/LGTV-Keys.js:4:13)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
    at node:internal/main/run_main_module:28:49

```

Here is the github link to the node module I am using... some day would love to create NR palette wrapper for this [https://github.com/WesSouza/lgtv-ip-control/tree/main](https://github.com/WesSouza/lgtv-ip-control/tree/main)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [12 December 2023 16:29 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/2 "2023-12-12T16:29:49Z")

</div>

What have you done to try and create `Keys`, I can't see it in the code you've shared. So not really possible to help without that.

Did you mean to do `Object.keys(TV).forEach(key => console.log("Key:", key))`?

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [12 December 2023 17:04 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/3 "2023-12-12T17:04:46Z")

</div>

Because `Keys` are not imported:

```js
const { LGTV, Keys } = require('lgtv-ip-control');
const TV = new LGTV('x.x.x.x', 'xx:xx:xx:xx:xx:xx', 'XXXXXXXX');

Object.keys(Keys).forEach(key => console.log("Key:", key));

```

I don't really know if you should use `Object.keys` or `Object.values` for an enum 🤔

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [12 December 2023 17:12 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/4 "2023-12-12T17:12:59Z")

</div>

Fun Fact:  
JavaScript doesn't have enums, I mean you can "pretend" and disguise them as enums using  
typescript - but during runtime - they are just an object with `key:value` pairs.

Anyway, as your were

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [12 December 2023 17:15 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/5 "2023-12-12T17:15:20Z")

</div>

Yes, I HATE TypeScript. 🙂

You beat me to the draw Marcus 😀

So it should indeed be `Object.keys` to get all the keys of the Keys and `Object.values` to get all of the values of the Keys - and that isn't confusing at all is it! 🤣

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [13 December 2023 02:29 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/6 "2023-12-13T02:29:20Z")

</div>

If you look at the code reference information in github the following is defined...

```auto
export enum Keys {
  arrowDown = 'arrowdown',
  arrowLeft = 'arrowleft',
  arrowRight = 'arrowright',
  arrowUp = 'arrowup',
  aspectRatio = 'aspectratio',
  audioMode = 'audiomode',
  back = 'returnback',
  blueButton = 'bluebutton',
  captionSubtitle = 'captionsubtitle',
  channelDown = 'channeldown',
  channelList = 'channellist',
  channelUp = 'channelup',
  deviceInput = 'deviceinput',
  energySaving = 'screenbright',
  fastForward = 'fastforward',
  greenButton = 'greenbutton',
  home = 'myapp',
  info = 'programminfo',
  liveTV = 'livetv',
  menu = 'settingmenu',
  number0 = 'number0',
  number1 = 'number1',
  number2 = 'number2',
  number3 = 'number3',
  number4 = 'number4',
  number5 = 'number5',
  number6 = 'number6',
  number7 = 'number7',
  number8 = 'number8',
  number9 = 'number9',
  ok = 'ok',
  play = 'play',
  previousChannel = 'previouschannel',
  programGuide = 'programguide',
  record = 'record',
  redButton = 'redbutton',
  rewind = 'rewind',
  sleepTimer = 'sleepreserve',
  userGuide = 'userguide',
  videoMode = 'videomode',
  volumeDown = 'volumedown',
  volumeMute = 'volumemute',
  volumeUp = 'volumeup',
  yellowButton = 'yellowbutton',
}

```

So that is not an exported enum 'Keys'?

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [13 December 2023 08:12 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/7 "2023-12-13T08:12:03Z")

</div>

> [@Nodi.Rubrum](#):
>
> So that is not an exported enum 'Keys'?

No. This is a TypeScript construct - which is translated into Javascript during the build process.  
It is as @marcus-j-davies explained:

> [@marcus-j-davies](#):
>
> JavaScript doesn't have enums

When you look into your `./dist/constants` directory, you'll find in `TV.js` the result of that build process as

```auto
var Keys;
(function(Keys2) {
  Keys2["arrowDown"] = "arrowdown";
  Keys2["arrowLeft"] = "arrowleft";
  Keys2["arrowRight"] = "arrowright";
  Keys2["arrowUp"] = "arrowup";
  Keys2["aspectRatio"] = "aspectratio";
  Keys2["audioMode"] = "audiomode";
  Keys2["back"] = "returnback";
  Keys2["blueButton"] = "bluebutton";
  Keys2["captionSubtitle"] = "captionsubtitle";
  Keys2["channelDown"] = "channeldown";
  Keys2["channelList"] = "channellist";
  Keys2["channelUp"] = "channelup";
  Keys2["deviceInput"] = "deviceinput";
  Keys2["energySaving"] = "screenbright";
  Keys2["fastForward"] = "fastforward";
  Keys2["greenButton"] = "greenbutton";
  Keys2["home"] = "myapp";
  Keys2["info"] = "programminfo";
  Keys2["liveTV"] = "livetv";
  Keys2["menu"] = "settingmenu";
  Keys2["number0"] = "number0";
  Keys2["number1"] = "number1";
  Keys2["number2"] = "number2";
  Keys2["number3"] = "number3";
  Keys2["number4"] = "number4";
  Keys2["number5"] = "number5";
  Keys2["number6"] = "number6";
  Keys2["number7"] = "number7";
  Keys2["number8"] = "number8";
  Keys2["number9"] = "number9";
  Keys2["ok"] = "ok";
  Keys2["play"] = "play";
  Keys2["previousChannel"] = "previouschannel";
  Keys2["programGuide"] = "programguide";
  Keys2["record"] = "record";
  Keys2["redButton"] = "redbutton";
  Keys2["rewind"] = "rewind";
  Keys2["sleepTimer"] = "sleepreserve";
  Keys2["userGuide"] = "userguide";
  Keys2["videoMode"] = "videomode";
  Keys2["volumeDown"] = "volumedown";
  Keys2["volumeMute"] = "volumemute";
  Keys2["volumeUp"] = "volumeup";
  Keys2["yellowButton"] = "yellowbutton";
})(Keys || (Keys = {}));

```

which in essence is the definition sequence of a Javascript `object`.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [13 December 2023 09:25 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/8 "2023-12-13T09:25:45Z")

</div>

So @Nodi.Rubrum, is it good?

The reason we don't know if you should use `Object.keys` or `Object.values` is that it depends on what you want to do:

- key: is the label, a friendlier name
- value: is the value the code expects

As Marcus and Ralph said an enum is a TypeScript construct that is translated into an object in javascript. But it's used the same way:

```js
const cmd = Keys.back;
// Expected: returnback

TV.sendKey(cmd);
...

```

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [14 December 2023 00:31 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/9 "2023-12-14T00:31:17Z")

</div>

Interesting, clearly the code suggests 'exported' enum. And I am or was thinking of enums comparable C++ or C#, where defined types are reference-able when library/module is loaded. And, yes, I wanted to reference value by key name. Could not understand by Key.back did not result in 'returnback' value when executing code via node (CLI). Javascript, or typescript, not as familiar with it as other scripting engines, say python, php or even powershell.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [14 December 2023 07:40 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/10 "2023-12-14T07:40:52Z")

</div>

> [@Nodi.Rubrum](#):
>
> enums comparable C++ or C#,

I am a c# developer by trade - hence I also had to get my head around this enum / not enum thing when I started my venture into typescript

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [14 December 2023 09:21 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/11 "2023-12-14T09:21:43Z")

</div>

And not forgetting that Typescript and C# are Microsoft inventions. Typescript was invented to make JavaScript look like C# to try to lock more developers into the Microsoft world. (my cynical view) Hence why it has things that look like C# 😀

I don't like it because it is yet another abstraction from JavaScript and will always end up with edge-cases and issues. Might as well invest the time in getting properly to grips with JavaScript in my view.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [14 December 2023 10:05 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/12 "2023-12-14T10:05:51Z")

</div>

> [@TotallyInformation](#):
>
> Might as well invest the time in getting properly to grips with JavaScript in my view.

Partially agree.

If a project is not going to involve other contributions, and you’re the only developer - I would indeed not consider typescript.

But when a project is subject to various contributions I think typescript is very strong here, as during design time, errors are highlighted, types are checked for the contributor who may not know the code base etc etc.

I think coming from c# where types are strict, I do see (and use) typescript and its benefits.

But as said, if I was the only developer in a js project, I would not use typescript

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [14 December 2023 11:52 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/13 "2023-12-14T11:52:15Z")

</div>

> [@marcus-j-davies](#):
>
> I think coming from c#

Did I mention how much I hate C/C++/C# - I may be a little biased. 🤣

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [22 December 2023 17:20 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/14 "2023-12-22T17:20:19Z")

</div>

I did quite a bit of C development a long long time ago... there was an explicit elegance to the power of C. But you had to be disciplined and explicit in design and code implementation of course. No variant of C is forgiving.

Javascript is giving me fits... but I am learning. I some times miss the simplistic early VB and HyperCard (Yes HyperCard, if you know what that was?).

I have yet to significantly tackled async python methods or async javascript methods, but I see that in the distance!

Reminds me of something a developer manager at a fortune 10 firm said, "Why does everyone [in my team] have a hard-on for Java!" That was said in 1996 if I recall right... Wow, if only he had known where Java was going to go, lead, etc.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 February 2024 17:20 UTC](https://discourse.nodered.org/t/so-trick-to-reference-exported-enum/83644/15 "2024-02-20T17:20:30Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
