title: Why can't TypeScript Objects index with generic types?
body: <p>In JavaScript, plain old objects can take all kinds of abuse. For example you can write:</p>
  <pre class="lang-js prettyprint-override"><code>var obj = {};
  var func = new Function() {};
  obj[func] = 5; // totally fine
  obj[func]; // returns 5
  </code></pre>
  <p>With some creativity you could write a generic class <code>SimpleSet&lt;T&gt;</code> that might use an object as its backing store in JavaScript or TypeScript. One straightforward way may involve writing something like this:</p>
  <pre class="lang-typescript prettyprint-override"><code>public add(val: T) {
    this._dict[val] = val;
  }
  </code></pre>
  <p>Which doesn't work in TypeScript. <strong>Why?</strong> More permissive versions of <code>add</code> work, see <code>add2</code> and <code>add3</code>.</p>
  <pre class="lang-typescript prettyprint-override"><code>class SimpleSet&lt;T&gt; {
    _dict: Object;
    constructor() {
  
    }
  
    // error:
    public add(val: T) {
      this._dict[val] = val;
    }
  
    // no error:    
    public add2(val: T) {
      this._dict[val as any] = val;
    }
  
    // no error:
    public add3(val: any) {
      this._dict[val] = val;
    }
  }
  </code></pre>
  <p>With <code>add</code>, two errors appear:</p>
  <blockquote>
  <p>Type 'T' is not assignable to type 'Object[T]'.</p>
  <p>Type 'T' cannot be used to index type 'Object'.</p>
  </blockquote>
  <p><a href="https://i.stack.imgur.com/n6MFN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n6MFN.png" alt="enter image description here" /></a></p>
  <h2><strong>Why?</strong></h2>