1

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?

1
  • add an export to your enum - export enum countryCode{} Commented Aug 5, 2017 at 5:13

1 Answer 1

3

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';
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

when i do import - should i import a namespace or enum? strange - in app.component.ts import is working, but in @Injectable() export class UserData with exactly same import - i get the error Cannot find name 'CountryCode'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.