Home > Blockchain >  How to declare multiple enum in a single TypeScript file and return under single export statement (s
How to declare multiple enum in a single TypeScript file and return under single export statement (s

Time:06-21

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:

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 remain private and can only be accessed by Enums.Actions or Enums.Groups? By private I mean, someone else should not be able to access them directly.

What is the suggested way to do this?

CodePudding user response:

You can declare both enums in a single file (Enums.ts):

enum Groups {
    Lead= "Lead",
    Permanent= "Permanent"
}

enum Actions {
    Delete = "Delete",
    Update = "Update"
}

export { Groups, Actions };

and then use below import statement in another file:

import * as Enums from "./Enums";

which allows you to reference your enums in a following way:

Enums.Actions.Delete

CodePudding user response:

you can do it like this:

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

  export enum Groups {
    Lead = "Lead",
    Permanent = "Permanent"
  }
}
  • Related