Use object literal as TypeScript enum values

前端 未结 6 1330
星月不相逢
星月不相逢 2020-12-29 17:54

I have an enum:

export enum PizzaSize {
  SMALL =  0,
  MEDIUM = 1,
  LARGE = 2
}

But here I\'d like t

6条回答
  •  不知归路
    2020-12-29 18:38

    You can use a typed const to achieve this:

    export const PizzaSize: {
        [key: string]: { key: string, value: string };
    } = {
        SMALL: { key: 'key', value: 'value' }
    };
    

    Optionally you can extract the type information to separate interface declarations:

    
    interface PizzaSizeEnumInstance {
        key: string;
        value: string;
    }
    
    interface PizzaSizeEnum {
        [key: string]: PizzaSizeEnumInstance;
    }
    
    export const PizzaSize: PizzaSizeEnum = {
        SMALL: { key: 'key', value: 'value' }
    };
    

提交回复
热议问题