[Fixed] Use object literal as TypeScript enum values


I have an enum:

export enum PizzaSize {
  SMALL =  0,
  MEDIUM = 1,
  LARGE = 2

But here I’d like to use some pair of values: e.g. SMALL I would like to say that it has a key of 0 and a value of 100. I endeavor to use:

export enum PizzaSize {
  SMALL =  { key: 0, value: 100 },
  // ...

But TypeScript doesn’t accept this one. How can I do this?


Update: find @Javarome’s answer below, which is more elegant. I suggest using his way.

If you need to use Type, try adding some code.
usage: getPizzSizeSpec(PizzaSize.small).value

enum PizzaSize {
interface PizzaSizeSpec {
    key: number,
    value: number
function getPizzaSizeSpec(pizzaSize: PizzaSize): PizzaSizeSpec {
    switch (pizzaSize) {
        case PizzaSize.small:
            return {key:0, value: 25};
        case PizzaSize.medium:
            return {key:0, value: 35};
        case PizzaSize.large:
            return {key:0, value: 50};

Leave a Reply

(*) Required, Your email will not be published