0
votes

Can you tell if it possible to create enum like this in Typescript?

public enum FooEnum {

    ITEM_A(1), ITEM_B(2), ITEM_C(3);

    private int order;

    private FooEnum (int order) {
        this.order = order;
    }

    public int getOrder() {
        return order;
    }
}

I have enum like this:

export enum FooEnum {
  ITEM_A = 'ITEM_A',
  ITEM_B = 'ITEM_B',
  ITEM_C = 'ITEM_C',
}

which I am using in TypeORM entities

@Column({ type: 'enum', enum: FooEnum })
foo!: FooEnum

I need to assign enum values to numbers to define their priority. Is possible to do that?

I had also idea to create value object with constants like you can see below but I don't know to use this class on entity, to still save Foo.ITEM_A like 'ITEM_A' string

class Foo {
  public static ITEM_A = new Country(1);
  public static ITEM_B = new Country(2);
  public static ITEM_C = new Country(3);

  constructor(order: number) {
    this.order = order;
  }

  readonly order: number;
}
1
What does Java have to do with this?Robert Harvey
@RobertHarvey because the question pertains to porting Java code to TypeScript. Not all web devs are Java devs.Mr. Polywhirl
You can create a class with readonly properties and make their instances readonly. See: Recreating advanced Enum types in TypescriptMr. Polywhirl

1 Answers

2
votes

This article describes a way to encapsulate static readonly instance variables using TypeScript.

"Recreating advanced Enum types in Typescript"

And here is the complete Gist (with comments):

GitHub Gist / NitzanHen / ts-enums-complete.ts

Here is an example Country "enum" class:

class Country {
  static readonly FRANCE = new Country('FRANCE', 1);
  static readonly GERMANY = new Country('GERMANY', 2);
  static readonly ITALY = new Country('ITALY', 3);
  static readonly SPAIN = new Country('SPAIN', 4);

  static get values(): Country[] {
    return [
      this.FRANCE,
      this.GERMANY,
      this.ITALY,
      this.SPAIN
    ];
  }

  static fromString(name: string): Country {
    const value = (this as any)[name];
    if (value) return value;
    const cls: string = (this as any).prototype.constructor.name;
    throw new RangeError(`Illegal argument: ${name} is not a member of ${cls}`);
  }

  private constructor(
    public readonly name: string,
    public readonly order: number
  ) { }

  public toJSON() {
    return this.name;
  }
}

export default Country;

Usage

const selectedCountry: Country = Country.FRANCE;

console.log(selectedCountry.order);