2

I know I can have an Enum like this:

enum TShirtSize {
  Small = 3,
  Medium = 5,
  Large = 8
}

var mySize = TShirtSize.Large;

But is there any way that I could specify a text string instead of the numbers 3,5 and 8 ?

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

5 Answers5

8

You can create a class that you can use for purely string based enums. Demo:

class Enum {
    constructor(private value: string) { }
    /** For equality == with string */
    toString() {return this.value }
    /** For send to server serialization */
    toJSON() {return this.value }
}

class Foo extends Enum {    
    static First = new Foo('First')
}

// From server to browser
var recievedDTO: { something: Foo } = <any>{
    something: 'First'
}
console.log(recievedDTO.something == Foo.First); // true

// To server 
var sendDTO = {
    something: Foo.First,
}
console.log(JSON.stringify(sendDTO) == '{"something":"First"}'); // true

// Warning: don't use `===`
console.log(recievedDTO.something === Foo.First); // false 
basarat
  • 261,912
  • 58
  • 460
  • 511
1

No - Strings are not allowed as values for enums which are "subtypes of the Number primitve type".

This is covered in the TypeScript Language Specification under a few sections.

(From 9.1) The enum type is a distinct subtype of the Number primitive type.

(From 9.2) The body of an enum declaration defines zero or more enum members which are the named values of the enum type. Each enum member has an associated numeric value of the primitive type introduced by the enum declaration.

(From 9.2) Expressions specified for computed members must produce values of type Any, the Number primitive type, or the enum type itself.

However, note that restriction to integers is at least "advisable",

(From 9.2) A ConstantEnumMemberSection introduces one or more constant members with consecutive integral values starting at the specified constant value1

(From 1.7) Section 9 describes how programmers can also explicitly assign integers to enum members..

1 In the current TypeScript compiler if a non-integral number is specified then the the following members must be assigned a value explicitly; however it is allowed to use non-integrals as constants. This follows from the above LS rules that enums are subtype of number, not of a more refined "integer".

user2864740
  • 60,010
  • 15
  • 145
  • 220
1

If you want to be able to use the identifier as a string description; you can obtain both the value of the enum, which is an integer, and its name - here is an example:

enum TShirtSize {
  Small = 3,
  Medium = 5,
  Large = 8
}

// 8
var sizeValue = TShirtSize.Large;

// "Large"
var sizeName = TShirtSize[sizeValue];

You may also want to take a look at the type-safe enum pattern as this can be useful in certain circumstances.

Fenton
  • 241,084
  • 71
  • 387
  • 401
0

Use a dictionary for that:

var enumStrings: { [id: TShirtSize] : string; } = {};
enumStrings[TShirtSize.Small] = "Very Small";
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
0

Enum values are stored as integers, I don't think you can store string constants as any of its its value. However you can use Symbolic constants using # define, for example:

> # define small  "any string1"
> # define medium "any string2"

and so on..