2426021684 2426021684 - 6 months ago 46
Javascript Question

Creating a nested enum typescript/javascript

enum Main{
enum Sub{
P1
},
P1
}


How do I create a nested enum in typescript where
Main.Sub
,
Main.Sub.P1
, and
Main.P1
are all valid and are mapped to an integer or string.

Answer

How do I create a nested enum in typescript where Main.Sub, Main.Sub.P1, and Main.P1 are all valid

You can do it by taking advantage of namespaces:

enum Main {
    P1
}
namespace Main {
    export enum Sub {
        P1
    }
}
let main: Main;
main = Main.P1;
let mainSub: Main.Sub;
mainSub = Main.Sub.P1;

More

This definitely feels like abusing namespaces.

Comments