How to extend enum in TypeScript

From time to time you need to extend an enum in TypeScript, however, we can’t use extends construction like in case of interface.

enum MySuperEnum {
ONE,
TWO
}

enum MyMoreSuperEnum extends MySuperEnum // This is wrong

Of course, we can c…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sergey Egorov

From time to time you need to extend an enum in TypeScript, however, we can't use extends construction like in case of interface.

enum MySuperEnum {
  ONE,
  TWO
}

enum MyMoreSuperEnum extends MySuperEnum // This is wrong

Of course, we can create one more enum and then create union type:

enum MySuperEnumExtender {
  THREE
}

type MyUnionType = MySuperEnum | MySuperEnumExtender;

What we can get and why this approach is not good? MyUnionType is a type, we can't use it like enum to call its props.

So what we can do then?

First of all, we should use another and more preferable way of enum implementation with const. Why?

// describe an enum object
const  MySuperEnum = {
  ONE: 'ONE',
  TWO: 'TWO'
} as const

// create the MySuperEnum type
type MySuperEnum = typeof MySuperEnum[keyof typeof MySuperEnum];

What is as const? Please click to find out more.

Cool! Now our MySuperEnum is defined in a different way!

It is time to create new MyMoreSuperEnum that will be extended with MySuperEnum.

// describe an enum object
const  MyMoreSuperEnum = {
  ...MySuperEnum,
  THREE: 'THREE'
} as const

// create the MyMoreSuperEnum type
type MyMoreSuperEnum = typeof MyMoreSuperEnum[keyof typeof MyMoreSuperEnum];

Profit. Hope this trick will be useful for you.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sergey Egorov


Print Share Comment Cite Upload Translate Updates
APA

Sergey Egorov | Sciencx (2022-09-15T15:17:33+00:00) How to extend enum in TypeScript. Retrieved from https://www.scien.cx/2022/09/15/how-to-extend-enum-in-typescript/

MLA
" » How to extend enum in TypeScript." Sergey Egorov | Sciencx - Thursday September 15, 2022, https://www.scien.cx/2022/09/15/how-to-extend-enum-in-typescript/
HARVARD
Sergey Egorov | Sciencx Thursday September 15, 2022 » How to extend enum in TypeScript., viewed ,<https://www.scien.cx/2022/09/15/how-to-extend-enum-in-typescript/>
VANCOUVER
Sergey Egorov | Sciencx - » How to extend enum in TypeScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/15/how-to-extend-enum-in-typescript/
CHICAGO
" » How to extend enum in TypeScript." Sergey Egorov | Sciencx - Accessed . https://www.scien.cx/2022/09/15/how-to-extend-enum-in-typescript/
IEEE
" » How to extend enum in TypeScript." Sergey Egorov | Sciencx [Online]. Available: https://www.scien.cx/2022/09/15/how-to-extend-enum-in-typescript/. [Accessed: ]
rf:citation
» How to extend enum in TypeScript | Sergey Egorov | Sciencx | https://www.scien.cx/2022/09/15/how-to-extend-enum-in-typescript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.