title: Indexing into Typescript Record with opaque type
body: <p>The following code is giving me a TypeScript error:</p>
  <pre><code>type UserID = string &amp; { readonly _: unique symbol };
  
  interface Chat {
    name: string
  }
  
  type AllChats = Record&lt;UserID, Chat&gt;;
  
  const test: AllChats = {}
  const userID = &quot;ads&quot; as UserID;
  
  test[userID] = {
    name: &quot;my chat&quot;
  }
  </code></pre>
  <p>I'm using UserID as an opaque type (like described here <a href="https://evertpot.com/opaque-ts-types/" rel="nofollow noreferrer">https://evertpot.com/opaque-ts-types/</a>), so it acts a string, however random strings cannot be assigned to a variable of type UserID.</p>
  <p>However, I'm getting the following error on the last line: &quot;Element implicitly has an 'any' type because expression of type 'UserID' can't be used to index type 'Record&lt;UserID, Chat&gt;'.&quot;</p>
  <p>Any idea why I'm getting this error? I don't see why I can't use something of type UserID into index into a record of type Record&lt;UserID, Chat&gt;.</p>
  <p>UPDATE: According to this page <a href="https://levelup.gitconnected.com/building-type-safe-dictionaries-in-typescript-a072d750cbdf" rel="nofollow noreferrer">https://levelup.gitconnected.com/building-type-safe-dictionaries-in-typescript-a072d750cbdf</a>, it looks like I can achieve the behavior I want by using Javascript Maps instead of objects. But this means giving up on all the object syntax that Javascript offers, which I'd rather not do.</p>
  <p>UPDATE2: Looks like this might be a known issue <a href="https://github.com/microsoft/TypeScript/issues/15746" rel="nofollow noreferrer">https://github.com/microsoft/TypeScript/issues/15746</a> that Typescript doesn't handle right now</p>