Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>TypeScript 2.4</h2> <p>Now has string enums so your code just works: </p> <pre><code>enum E { hello = "hello", world = "world" }; </code></pre> <p></p> <h2>TypeScript 1.8</h2> <p>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). </p> <pre><code>type Options = "hello" | "world"; var foo: Options; foo = "hello"; // Okay foo = "asdf"; // Error! </code></pre> <p>More : <a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#string-literal-types" rel="noreferrer">https://www.typescriptlang.org/docs/handbook/advanced-types.html#string-literal-types</a></p> <h2>Legacy Support</h2> <p>Enums in TypeScript are number based. </p> <p>You can use a class with static members though: </p> <pre><code>class E { static hello = "hello"; static world = "world"; } </code></pre> <p>You could go plain as well: </p> <pre><code>var E = { hello: "hello", world: "world" } </code></pre> <p><strong>Update:</strong> Based on the requirement to be able to do something like <code>var test:E = E.hello;</code> the following satisfies this: </p> <pre><code>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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload