title: Typescript enum, interface and mapping
body: <p><strong>In TypeScript, in an array or similar data structure, how can I map a string to an id, while ensuring that only a certain range of ids is allowed?</strong></p>
  
  <p>Here is what I want to do. This works fine. However, I am wondering if there is a more concise way of achieving this?</p>
  
  <pre><code>enum ETypeId {
      alpha  = "a",
      beta   = "b",
      gamma  = "g"
  }
  
  interface IType {
      id:     ETypeId,
      title:  string,
  }
  
  myTypes: IType[] = [
      { id: ETypeId.alpha, title: "Alpha" },
      { id: ETypeId.beta,  title: "Beta"  },
      { id: ETypeId.gamma, title: "Gamma" }
  ];
  </code></pre>
  
  <p>As is, I have to do the following to get from the <code>id</code> to the <code>title</code>: </p>
  
  <pre><code>function getTypeForTypeId( typeId: ETypeId ): IType {
      return myTypes.find( type =&gt; type.id == typeId );
  }
  </code></pre>
  
  <p><strong>Can I use a different data structure that makes some of the above code more concise, or is this already as good as it gets?</strong>   </p>
  
  <hr>
  
  <p>Explanation:</p>
  
  <ul>
  <li><code>"a"</code> is what gets stored in my database</li>
  <li><code>ETypeId.alpha</code> is how I access it in my code</li>
  <li><code>"Alpha"</code> is what gets displayed to the user.</li>
  </ul>