Leo Messi
Leo Messi

Reputation: 6176

Is it possible to have nested enum in Typescript?

Having those enums:

export enum First {
  TEST_1 = 'test_1',
  TEST_2 = 'test_2'
}

export enum Second {
  TEST_3 = 'test_3',
  TEST_4 = 'test_4'
}

is it possible to combine them into a single one?

Something like:

export enum Main {
  First {
    TEST_1 = 'test_1',
    TEST_2 = 'test_2'
  },
  Second {
    TEST_3 = 'test_3',
    TEST_4 = 'test_4'
  }
}

Upvotes: 2

Views: 1210

Answers (1)

Ran Turner
Ran Turner

Reputation: 18046

You can’t create nested enums in TypeScript. What you could do is create a little hack where you can create an object (Main), which contains a bunch of enums and essentially achieve the same effect

enum First {
    TEST_1 = "Main.First.Test1",
    TEST_2 = "Main.First.Test2"
}

enum Second {
    TEST_3 = "Main.Second.Test3",
    TEST_4 = "Main.Second.Test4"
}

const Main = {
    First,
    Second
};

Upvotes: 2

Related Questions