Skip to content

keyOf enums + Enum classes #80

Closed
Closed
@michahell

Description

@michahell

We use a custom made json-schem to TS script which creates TitleCasedEnums classes with public static string = 'enumValue' fields. This lacks actual Enum behaviour but does provide for easy enum accessibility. We were looking into enhancing this with the TS 2.1 keyof property.

Right now, we have something like this:

export interface ExampleSchema {
  firstName: string;
  lastName: string;
  /**
   * Age in years
   */
  age?: number;
  hairColor?: string;
}

export class HairColorEnum {
  public static BLACK: string = 'black';
  public static BROWN: string = 'brown';
  public static BLUE: string = 'blue';
}

This would already be a lot better:

export interface ExampleSchema {
  firstName: string;
  lastName: string;
  /**
   * Age in years
   */
  age?: number;
  hairColor?: (HairColorEnum.BLACK | HairColorEnum.BROWN | HairColorEnum.BLUE);
}

export class HairColorEnum {
  public static BLACK: string = 'black';
  public static BROWN: string = 'brown';
  public static BLUE: string = 'blue';
}

But this would be best:

export interface ExampleSchema {
  firstName: string;
  lastName: string;
  /**
   * Age in years
   */
  age?: number;
  hairColor?: keyof HairColorEnum;
}

export class HairColorEnum {
  public static BLACK: string = 'black';
  public static BROWN: string = 'brown';
  public static BLUE: string = 'blue';
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions