Use object in typescript enumeration

I have an enumeration:

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

But here I would like to use a couple of values: for example. SMALL I would like to say that it has 2 values ​​(0, 100). How can I do it?

I try to use

 export enum PizzaSize { SMALL = {key:key, value: value}, ... } 

But typescript does not accept this.

+31
source share
5 answers

Update: find the @Javarome answer below, which is more elegant. I suggest using his path.

If you need to use Type, try adding code. usage: getPizzSizeSpec(PizzaSize.small).value

 enum PizzaSize { small, medium, large } interface PizzaSizeSpec { key: number, value: number } function getPizzaSizeSpec(pizzaSize: PizzaSize): PizzaSizeSpec { switch (pizzaSize) { case PizzaSize.small: return {key:0, value: 25}; case PizzaSize.medium: return {key:0, value: 35}; case PizzaSize.large: return {key:0, value: 50}; } } 
+14
source

TypeScript only supports numeric or string enumerations , so you must emulate enumerations of objects using a class (which allows you to use it as a type in a function declaration):

 export class PizzaSize { static readonly SMALL = new PizzaSize('SMALL', 'A small pizza'); static readonly MEDIUM = new PizzaSize('MEDIUM', 'A medium pizza'); static readonly LARGE = new PizzaSize('LARGE', 'A large pizza'); // private to disallow creating other instances of this type private constructor(private readonly key: string, public readonly value: any) { } toString() { return this.key; } } 

then you can use predefined instances to access their value :

 const mediumVal = PizzaSize.MEDIUM.value; 

or any other type of property / property that you can define in PizzaSize .

and thanks to overriding toString() you can also implicitly print the enum name / key from the object:

 console.log(PizzaSize.MEDIUM); // prints 'MEDIUM' 
+43
source

Try using:

 const pizzaSize = { small: { key: 0, value: 25 }, medium: { key: 1, value: 35 }, large: { key: 2, value: 50 } } 
+5
source

Starting with Typescript 3.4 , you can use a combination of keyof typeof and const statements to create objects that can have the same type safety as enumerations, and yet contain complex values.

By creating type with the same name as const , you can have the same exhaustibility checks as regular enums.

The only problem is that you need some key in a complex object (here I use value ) to store the name of the enum member (if someone can figure out a helper function that can create these objects in a safe way, I would like to see this ! I could not get to work).

 export const PizzaSize = { small: { value: 'small', key: 0, size: 25 }, medium: { value: 'medium', key: 1, size: 35 }, large: { value: 'large', key: 2, size: 50 }, } as const export type PizzaSize = keyof typeof PizzaSize // if you remove any of these cases, the function won't compile // because it can't guarantee that you've returned a string export function order(p: PizzaSize): string { switch (p) { case PizzaSize.small.value: return 'just for show' case PizzaSize.medium.value: return 'just for show' case PizzaSize.large.value: return 'just for show' } } // you can also just hardcode the strings, // they'll be type checked export function order(p: PizzaSize): string { switch (p) { case 'small': return 'just for show' case 'medium': return 'just for show' case 'large': return 'just for show' } } 

In other files this can be used simply, just import PizzaSize .

 import { PizzaSize } from './pizza' console.log(PizzaSize.small.key) type Order = { size: PizzaSize, person: string } 

Also note that even objects that are usually mutable cannot be modified using the as const syntax.

 const Thing = { ONE: { one: [1, 2, 3] } } as const // this won't compile!! Yay!! Thing.ONE.one.splice(1, 0, 0) 
+4
source

Object.freeze makes it read-only and prevents the addition of additional properties:

 const pizzaSize = Object.freeze({ small: { key: 0, value: 25 }, medium: { key: 1, value: 35 }, large: { key: 2, value: 50 } }) 
-one
source

Source: https://habr.com/ru/post/1013210/


All Articles