Enums with explicit values

By default all enum values are resolved to numbers. Let’s say if you have something like

enum MimeType {

the real value behind e.g. MimeType.PDF will be 2.

But some of the time it is important to have the enum resolve to a different type. E.g. you receive the value from
backend / frontend / another system which is definitely a string. This could be a pain, but luckily there is this

enum MimeType {
JPEG = <any>'image/jpeg'
PNG = <any>'image/png',
PDF = <any>'application/pdf'

This resolves the MimeType.PDF to application/pdf.

Since TypeScript 2.4 it’s possible to declare string enums:

enum MimeType {
JPEG = 'image/jpeg',
PNG = 'image/png',
PDF = 'application/pdf',

You can explicitly provide numeric values using the same method

enum MyType {
Value = 3,
ValueEx = 30,
ValueEx2 = 300

Fancier types also work, since non-const enums are real objects at runtime, for example

enum FancyType {
OneArr = <any>[1],
TwoArr = <any>[2, 2],
TwoArr = <any>[3, 3, 3]


var FancyType;
(function (FancyType) {
FancyType[FancyType["OneArr"] = [1]] = "OneArr";
FancyType[FancyType["TwoArr"] = [2, 2]] = "TwoArr";
FancyType[FancyType["ThreeArr"] = [3, 3, 3]] = "ThreeArr";
})(FancyType || (FancyType = {}));

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Enums in TypeScript

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories


Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page


Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
    Strength indicator
    Log In | Lost Password