Is there any way to check if an object is a type of enum in TypeScript?

jcroll
enum Direction {
    Up = 1,
    Down,
    Left,
    Right
}

if (typeof Direction === 'enum') {
    // doesn't work
}

Any way to check if the above is in fact an enum?

Nitzan Tomer

Enums are compiled into simple objects that have two way mapping:

name => value
value => name

So the enum in your example compiles to:

var Direction;
(function (Direction) {
    Direction[Direction["Up"] = 1] = "Up";
    Direction[Direction["Down"] = 2] = "Down";
    Direction[Direction["Left"] = 3] = "Left";
    Direction[Direction["Right"] = 4] = "Right";
})(Direction || (Direction = {}));

So the typeof of Direction is a plain "object".
There's no way of knowing in runtime that the object is an enum, not without adding more fields to said object.


Edit

Sometimes you don't need to try to workaround a problem with a specific approach you have, you can maybe just change the approach.
In this case, you can use your own object:

interface Direction {
    Up: 1,
    Down: 2,
    Left: 3,
    Right: 4
}
const Direction = {
    Up: 1,
    Down: 2,
    Left: 3,
    Right: 4
} as Direction;

Or:

type Direction = { [name: string]: number };
const Direction = { ... } as Direction;

Then turning this object into an array should be simple.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Simpler way to check literal object type in TypeScript

Is there a type for "any enum type" in TypeScript?

Typescript check for the 'any' type

Is there any way to target the plain JavaScript object type in TypeScript?

Is there any way of describing "an object type that cannot be empty" in typescript?

Typescript type of object literals of enum?

Avoid any type getting enum values in typescript

TypeScript set a type or interface to be any member of an enum

Typescript enum type check for conditional types?

Proper way to cast an enum to Type in Typescript

Typescript check if object matches type

What is the best way to check of an object is an Enum in Cython?

Best way to cast an object to a TypeScript Enum Value

How to check if a object is a type of enum in arraylist in java?

Are there any better way to remove this typescript code if check?

Typescript - way to check for existence of properties only and ignore type checking of the property itself without using any?

TypeScript - Check if given 'any' parameter is custom type?

Check if an object is a type of a generic type in Typescript

Check that object is not instance of any class in typescript

Is there any way to instantiate a Generic literal type in typescript?

Correct way to check if any object is a SyntheticEvent?

In scala, is there any way to check if an instance is a singleton object or not?

Is there any way to check whether an object is serializable or not in java?

correct way to represent any object in Typescript

TypeScript: Is there a way to check infinite nested array type?

Typescript check if property in object in typesafe way

TypeScript type that matches any object but not arrays

How to check the object type on runtime in TypeScript?

Type check between class and object literal in TypeScript