Is there a simple way to get a list of keys/numbers from enum in typescript?

typescript string enum, reverse mapping
get enum key from value
javascript enum get key by value
typescript string to enum
typescript iterate string enum
typescript enum in class
typescript enum from number
display enum name typescript

I would like a simple way to get a list of keys from an enumerator, I tried Object.keys, but it returned 6 keys, 0-5, I think it occours because when I tried foreach the enum, this returned keys and values list, is there a simple way to return only keys, I would not like to return to mount and return another list.

I will use this with react

My enum:

enum ObservationType {
    Accomodation = 1,
    Addtional = 2,
    LandTransport = 3

Sample that I tried and returned six values:

{Object.keys(ObservationType).map((type) => <div>{type}</div>)}

From the TypeScript enum documentation:

In this generated code, an enum is compiled into an object that stores both forward (name -> value) and reverse (value -> name) mappings. References to other enum members are always emitted as property accesses and never inlined.

Keep in mind that string enum members do not get a reverse mapping generated at all.

This means that if you're able, you may want to use string enums, e.g.:

enum ObservationType {
  Accomodation = 'Acc',
  Addtional = 'Addt',
  LandTransport = 'LandT',

// Object.keys will only have the three keys.

Another way to handle this would be to keep track of the mapped values to get the reverse mappings only once. This requires the numeric keys of the object to be set first which is not necessarily a guarantee:

enum ObservationTypeNums {

Object.keys(ObservationTypeNums).reduce((arr, key) => {
  if (!arr.includes(key)) {
  return arr;
}, []);

// arr now has only the string keys

Otherwise you will just have to filter out numeric keys in a way as you have done.

Enums � TypeScript, Using enums can make it easier to document intent, or create a set of distinct cases. Here, Up would have the value 0 , Down would have 1 , etc. of JavaScript's runtime behavior in a clever way, it's advised that you don't do this. Instead, use keyof typeof to get a Type that represents all Enum keys as strings. There is no way to compute the names of enum members. Object literals support computed names via square brackets. String-based enums # Instead of numbers, we can also use strings as enum member values: enum NoYes { No = 'No', Yes = 'Yes', } assert.equal(NoYes.No, 'No'); assert.equal(NoYes.Yes, 'Yes');

Try using ts-enum-util (github, npm):

import {$enum} from "ts-enum-util";

enum ObservationType {
    Accomodation = 1,
    Addtional = 2,
    LandTransport = 3

// type: ObservationType[]
// value: [1, 2, 3]
const values = $enum(ObservationType).getValues();

// type: ("Accomodation" | "Addtional" | "LandTransport")[]
// value: ["Accomodation", "Addtional", "LandTransport"]
const keys = $enum(ObservationType).getKeys();

// directly map the enum value/key pairs
const valueDivs = $enum(ObservationType).map(
    (value, key) => <div>{value}</div>

It takes care of ignoring the numeric key reverse lookup entries in the runt-time enum object for you, among other things.

Get a list with all values from an Enum � Issue #17198 � microsoft , A number enum will map from keys to values and from values to keys, so you must be careful: in case of string enum, simple Object.keys should do it there is no way to get a list of all values without changing const enum to a plain old enum . Typescript - The rough cut. jsdrupal/drupal-admin-ui#526. With string enums this is easy because there is no reverse mapping from values to keys, so Object.keys(Colour).map(k => Colour[k as any]) is ['RED', 'GREEN']. A number enum will map from keys to values and from values to keys, so you must be careful:

The ObservationType object will have keys :Accomodation, Additional, LandTransport, 1, 2, 3 and is fundamental to how enums work in TypeScript.

That said if you want just the string names you can with a simple filter:

{Object.keys(ObservationType).filter(k => typeof key === 'string').map((type) => <div>{type}</div>)}

What do need know about enumerations in TypeScript?, The TypeScript use self-invoking anonymous functions to create user types Enumeration is simple object which has object prototype. }step 0: var Names = undefined; Don't use for-in construction to get enumeration keys, because she Then let develop method to get array of { key, value } objects. /** TypeScript Data Type - Enum. Enums or enumerations are a new data type supported in TypeScript. Most object-oriented languages like Java and C# use enums. This is now available in TypeScript too. In simple words, enums allow us to declare a set of named constants i.e. a collection of related values that can be numeric or string values.

How about, Object.keys(ObservationType).filter(k => isNaN(Number(k)))

credit to @basarat whose answer I built on.

Enum in TypeScript, In simple words, enums allow us to declare a set of named constants i.e. a collection of Numeric enums are number-based enums i.e. they store string values as numbers. In the above example, we have an enum named PrintMedia. Let's see how TypeScript implements reverse mapping using the following example. In addition to creating an object with property names for members, numeric enums members also get a reverse mapping from enum values to enum names. For example, in this example: enum Enum { A } let a = Enum.A; let nameOfA = Enum[a]; // "A" TypeScript might compile this down to something like the following JavaScript:

TypeScript - Numeric Enums, At the same time each enum is given string values by their numeric keys key=1, value=CENTER Following example shows how to get all enum values: " number"); console.log(values); //getting all string values let sValues: Mockito - Verifying Method Calls � Spring MVC - Post Request With Simple� keyof and Lookup Types in TypeScript January 6, 2017. JavaScript is a highly dynamic language. It can be tricky sometimes to capture the semantics of certain operations in a static type system. Take a simple prop function, for instance: function prop(obj, key) { return obj[key]; }

Enums - TypeScript Deep Dive, Changing the number associated with a number enum. Enums An enum is a way to organize a collection of related values. Many other programming languages (C/C#/Java) have an enum data type but JavaScript does not. However, TypeScript does. You can use these values to do simple string comparisons. e.g.. TypeScript introduces the concept of arrays to tackle the same. An array is a homogenous collection of values. To simplify, an array is a collection of values of the same data type. It is a user defined type. Features of an Array. Here is a list of the features of an array − An array declaration allocates sequential memory blocks. Arrays are

How to get keys from enum without converting to any? : typescript, export function enumToKeys(_enum: Record<string, unknown>): string[] { let keys : string[] Object.keys, filter on number/not number (been a while since I've done it so I can't really That's especially dangerous if the names of your enum are not the same as their values. I'm sure this is just some easy thing I overloooked. The most basic datatype is the simple true/false value, which JavaScript and TypeScript call a boolean value. let isDone: boolean = false; Number # As in JavaScript, all numbers in TypeScript are floating point values. These floating point numbers get the type number. In addition to hexadecimal and decimal literals, TypeScript also supports

  • I got a solution, but I wonder if I can do it better. My solution: Object.keys(ObservationType).filter(o => !isNaN(o as any))
  • Why are you trying to get these keys?
  • I need to show some fields for each observation type with some particularities
  • Thanks basarat, it's like my solution that I posted as a comment to my post, my target was get only number values, so I did it: Object.keys(ObservationType).filter(o => !isNaN(o as any))
  • isNaN to check number is a bad idea. It should be used for NaN (not a number is a special value)
  • This won't work. Object.keys(['a', 'b', 'c']).filter(key => typeof key === 'string') will yield ['0', '1', '2']. All object keys are strings or symbols in JavaScript. Also, there's a typo in your code (k vs. key).
  • @philraj is correct, the check should be done with Number.isInteger, because the runtime type of all the keys of an object is always string.