Skip to content Skip to sidebar Skip to footer

Typescript Export Enum Based On The Condition?

Angular based on the environment I need to export enum. I don't know wether it is correct or not? role.ts import { environment } from '../../environments/environment'; if(environme

Solution 1:

You can do it like this:

import { environment } from'../../environments/environment';
exportclassRole {
  staticUser = 'user';
  staticRole = (environment.production) ? 'role' : 'admin';
}

Solution 2:

String emuns like:

exportenum Role {
       User = 'user',
       Admin = 'admin',
   }

Will be built into:

     "use strict";
   Object.defineProperty(exports, "__esModule", { value: true });
   varRole;
   (function (Role) {
       Role["User"] = "user";
       Role["Admin"] = "admin";
   })(Role = exports.Role || (exports.Role = {}));

So as you can see in the end your enum would be an object. You can rewrite your code like this

import { environment } from'../environments/environment';

    exportconstRoles = getRole();

    functiongetRole() {
     if (environment.production) {
       return {
         User: 'user',
         Admin: 'admin'
       };
     }

     return {
       User: 'user',
       Admin: 'user'
     };
    }

Post a Comment for "Typescript Export Enum Based On The Condition?"