Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to declare multiple enum in a single TypeScript file and return under single export statement (so that not exposing individual enums directly)

I want to have multiple enums in the same file and export them under single export statement. and when I import this single file in another file, I can access any particular enum as required.

What I current have: 2 seperate .ts file for Actions and Groups enums,

First .ts file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

export enum Actions {
   Delete: "Delete",
   Update: "Update"
}

Second .ts file:

export enum Groups {
   Lead: "Lead",
   Permanent: "Permanent"
}

Expected: I want a single .ts file and a single export statement. Using this export, I can access my both the enums. something like: Enums.Actions or Enums.Groups.

Groups, Actions should only be accessed by Enums.Actions or Enums.Groups? some other file should not be able to access individual enums directly.

What is the suggested way to do this?

>Solution :

you can do it like this:

export namespace Enum {
  export enum Actions {
    Delete = "Delete",
    Update = "Update"
  }

  export enum Groups {
    Lead = "Lead",
    Permanent = "Permanent"
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading