user1935987
user1935987

Reputation: 3347

Angular 4 export enum with the namespace

I'm trying to make a enum to handle country & currency codes. The enum must be used through entire app (Ionic 3 Angular 4 app).

So far, i found this way:

enum CountryCode {
  TH,
  BGD,
}

namespace CountryCode {
  export function getCurrencyCode(country: CountryCode) {
    switch (country) {
      case CountryCode.TH:
        return 'THB';
      case CountryCode.BGD:
        return 'BDT';
      default:
        return 'THB';
    }
  }
}

however in this case the enum can't be exported to other modules.

How can i solve this problem?

Upvotes: 1

Views: 1466

Answers (1)

Aravind
Aravind

Reputation: 41571

You should be declaring it inside the namespace as below,

export namespace CountryCode {
    export enum CountryCode {
        TH,
        BGD,
    }
    export function getCurrencyCode(country: CountryCode) {
        switch (country) {
            case CountryCode.TH:
                return 'THB';
            case CountryCode.BGD:
                return 'BDT';
            default:
                return 'THB';
        }
    }
}

Upvotes: 3

Related Questions