1

I have the following enum:

export enum ContentState {
    unstable = 'UNSTABLE',
    draft = 'DRAFT',
    stable = 'STABLE',
}

I want to check if a given enum within my program is STABLE. I want something like

data.myState.isStable()

and the check is done like this===ContentState.stable. I know I can do it within the code but I would be curious if I could spice up the enums.

I thought if I use as well

export namespace ContentState {
    const isReleasedStatus = (state: ContentState) => state === ContentState.STABLE;
}

but than I need to bypass the enum by itself. PLUS it is not imported into another class.

LeO
  • 4,238
  • 4
  • 48
  • 88
  • Does this answer your question? [Add functions to an Enum](https://stackoverflow.com/questions/28150739/add-functions-to-an-enum) – Peter Csala Apr 12 '21 at 13:19
  • 1
    I found this as well. But the problem with the accepted answer is that it doesn't encapsulate the `enum` and the function in an own class - which can be done as proposed therein. The general problem is that the function requires an argument with typeof `enum` whereas I am asking for NOT using bypassing it - more using it from 'within'. – LeO Apr 12 '21 at 14:06

0 Answers0