2

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'
  }
}
2
  • 1
    Only numeric enums can have computed members, but this expression has type '{ TEST_1: string; TEST_2: string; }'. If you do not need exhaustiveness checks, consider using an object literal instead. listen to ts :)) Commented Jan 8, 2022 at 19:50
  • What is the goal of the “nesting”? Commented Jan 8, 2022 at 21:30

1 Answer 1

2

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
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.