1

How to assign multiple value to an enum in typescript ? my enum is:

export enum TaxabilityType {
  Yes = 'TAXABLE',
  No = 'NON_TAXABLE',
  Unknown = 'MAYBE'
}

But I do not need 'Unknown' and for the value 'MAYBE' I want 'No' so I need something like:

  export enum TaxabilityType {
      Yes = 'TAXABLE',
      No = 'NON_TAXABLE' || 'MAYBE'

    }

How can I achieve it ?

3
  • Why not Having an Object instead of a Enum? Commented Aug 12, 2019 at 13:42
  • 3
    What do you want to use this for? Or better yet, how do you imagine you'd use this? Commented Aug 12, 2019 at 13:43
  • It can be done if the enum is used for flags but not for strings. You can see an example of combined flags in this article. Commented Aug 12, 2019 at 15:01

2 Answers 2

8

You can't.

An enum is a "set of named constants." (source)

A constant can't be two values.

Sign up to request clarification or add additional context in comments.

Comments

5

If you have to parse that enum from a datasource that has three different values you could do:

 export enum TaxabilityType {
  Yes = 'TAXABLE',
  No = 'NON_TAXABLE',
}

function getTaxability(taxability: "TAXABLE" | "NON_TAXABLE" | "MAYBE"): TaxabilityType {
 if(taxability === "MAYBE") return TaxabilityType.No;
 return TaxabilityType[taxability];
}

Comments

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.