Create an enum with string values
TypeScript 2.4
Now has string enums so your code just works:
enum E {
hello = "hello",
world = "world"
};
🌹
TypeScript 1.8
Since TypeScript 1.8 you can use string literal types to provide a reliable and safe experience for named string values (which is partially what enums are used for).
type Options = "hello" | "world";
var foo: Options;
foo = "hello"; // Okay
foo = "asdf"; // Error!
More : https://www.typescriptlang.org/docs/handbook/advanced-types.html#string-literal-types
Legacy Support
Enums in TypeScript are number based.
You can use a class with static members though:
class E
{
static hello = "hello";
static world = "world";
}
You could go plain as well:
var E = {
hello: "hello",
world: "world"
}
Update:
Based on the requirement to be able to do something like var test:E = E.hello;
the following satisfies this:
class E
{
// boilerplate
constructor(public value:string){
}
toString(){
return this.value;
}
// values
static hello = new E("hello");
static world = new E("world");
}
// Sample usage:
var first:E = E.hello;
var second:E = E.world;
var third:E = E.hello;
console.log("First value is: "+ first);
console.log(first===third);
In latest version (1.0RC) of TypeScript, you can use enums like this:
enum States {
New,
Active,
Disabled
}
// this will show message '0' which is number representation of enum member
alert(States.Active);
// this will show message 'Disabled' as string representation of enum member
alert(States[States.Disabled]);
Update 1
To get number value of enum member from string value, you can use this:
var str = "Active";
// this will show message '1'
alert(States[str]);
Update 2
In latest TypeScript 2.4, there was introduced string enums, like this:
enum ActionType {
AddUser = "ADD_USER",
DeleteUser = "DELETE_USER",
RenameUser = "RENAME_USER",
// Aliases
RemoveUser = DeleteUser,
}
For more info about TypeScript 2.4, read blog on MSDN.
TypeScript 2.4+
You can now assign string values directly to enum members:
enum Season {
Winter = "winter",
Spring = "spring",
Summer = "summer",
Fall = "fall"
}
See #15486 for more information.
TypeScript 1.8+
In TypeScript 1.8+, you can create a string literal type to define the type and an object with the same name for the list of values. It mimics a string enum's expected behaviour.
Here's an example:
type MyStringEnum = "member1" | "member2";
const MyStringEnum = {
Member1: "member1" as MyStringEnum,
Member2: "member2" as MyStringEnum
};
Which will work like a string enum:
// implicit typing example
let myVariable = MyStringEnum.Member1; // ok
myVariable = "member2"; // ok
myVariable = "some other value"; // error, desired
// explict typing example
let myExplicitlyTypedVariable: MyStringEnum;
myExplicitlyTypedVariable = MyStringEnum.Member1; // ok
myExplicitlyTypedVariable = "member2"; // ok
myExplicitlyTypedVariable = "some other value"; // error, desired
Make sure to type all the strings in the object! If you don't then in the first example above the variable would not be implicitly typed to MyStringEnum
.
In TypeScript 0.9.0.1, although it occurs a compiler error, the compiler can still compile the ts file into js file. The code works as we expected and Visual Studio 2012 can support for automatic code completion.
Update :
In syntax, TypeScript doesn't allow us to create an enum with string values, but we can hack the compiler :p
enum Link
{
LEARN = <any>'/Tutorial',
PLAY = <any>'/Playground',
GET_IT = <any>'/#Download',
RUN_IT = <any>'/Samples',
JOIN_IN = <any>'/#Community'
}
alert('Link.LEARN: ' + Link.LEARN);
alert('Link.PLAY: ' + Link.PLAY);
alert('Link.GET_IT: ' + Link.GET_IT);
alert('Link[\'/Samples\']: Link.' + Link['/Samples']);
alert('Link[\'/#Community\'] Link.' + Link['/#Community']);
Playground
TypeScript 2.1 +
Lookup types, introduced in TypeScript 2.1 allow another pattern for simulating string enums:
// String enums in TypeScript 2.1
const EntityType = {
Foo: 'Foo' as 'Foo',
Bar: 'Bar' as 'Bar'
};
function doIt(entity: keyof typeof EntityType) {
// ...
}
EntityType.Foo // 'Foo'
doIt(EntityType.Foo); // 👍
doIt(EntityType.Bar); // 👍
doIt('Foo'); // 👍
doIt('Bar'); // 👍
doIt('Baz'); // 🙁
TypeScript 2.4 +
With version 2.4, TypeScript introduced native support for string enums, so the solution above is not needed. From the TS docs:
enum Colors {
Red = "RED",
Green = "GREEN",
Blue = "BLUE",
}