title: Why am I getting "Type 'String' cannot be used as an index type" error in TypeScript?
body: <p>I have a large amount of JavaScript code that uses a number of associative arrays that allow very fast access to objects. So I tried to port this code and failed. Clearly, I don't understand TypeScript well enough yet.</p>
  <p>I have hunted for an answer to this question and found suggestions to create an interface etc., but this seems a bit off the track.</p>
  <p>So I tried the seemingly obvious:</p>
  <pre><code>     class Foo {
         allObjects: MyObject[];
          constructor() {
           this.allObjects = [];
         }
      }
  </code></pre>
  <p>where <code>MyObject</code> - simplified:</p>
  <pre><code>     class MyObject {
           _id: String;
           public getId(): String {
              return this._id;
           }
       }
  </code></pre>
  <p>And I would believe that:</p>
  <pre><code>     myObjectInstance: MyObject;
       fooInstance.allObjects[myObjectInstance.getId()] = myObjectInstance;
  </code></pre>
  <p>That would be fine... Yet I see the lovely <code>TS2342 error (An index expression argument must be of type 'string', 'number', or 'any')</code> which makes no sense to me since <code>getId()</code> is a <code>string</code>.  It doesn't seem like this should be that hard. Your help would be greatly appreciated.</p>