Closed
Description
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';
}