Searene
Searene

Reputation: 27624

Get the enum instance from the value

I have the following code.

enum Fruit {
  Apple = -1,
  Peach = 0,
  Orange = 1
}

const peach: Fruit = Fruit.fromValue(0); // I want to get a Fruit instance from 0, which means Peach.

There is no fromValue method in Enum in typescript, but I want to know if there is a method like that.

Upvotes: 1

Views: 46

Answers (1)

thesilican
thesilican

Reputation: 783

Seems like this actually works

enum Fruit {
  Apple = -1,
  Peach = 0,
  Orange = 1
}

const peach: Fruit = 0;

Upvotes: 1

Related Questions