Following code can be used to create an enum
in TypeScript:
enum e {
hello = 1,
world = 2
};
And the values can be accessed by:
e.hello;
e.world;
How do I create an enum
with string values?
enum e {
hello = "hello", // error: cannot convert string to e
world = "world" // error
};
Best Answer
TypeScript 2.4
Now has string enums so your code just works:
🌹
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).
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:
You could go plain as well:
Update: Based on the requirement to be able to do something like
var test:E = E.hello;
the following satisfies this: