title: TypeScript - How to access dictionary value by key?
body: <p>I have a dictionary of dictionaries which looks like this:</p>
  <pre><code>options: { language: string; value: { parameterId: string; parameterValue: string } }[];
  </code></pre>
  <p>When I inspect an instance of this at runtime, it looks like this:</p>
  <pre><code>{de: Object, en: Object}
  de:{1: &quot;-&quot;, 2: &quot;grün&quot;, 3: &quot;gelb&quot;, 4: &quot;rot&quot; }
  en:{1: &quot;-&quot;, 2: &quot;green&quot;, 3: &quot;yellow&quot;, 4: &quot;red&quot; }
  __proto__:{}
  </code></pre>
  <p>I am now trying to write a function which returns one of those dictionaries depending on these rules:</p>
  <ol>
  <li>If there is an entry with key &quot;-&quot;, return this entry.</li>
  <li>Else if there is an entry with the current browser language as key, return it.</li>
  <li>Else if there is an entry with key &quot;en&quot;, return it.</li>
  <li>Else return an empty dictionary.</li>
  </ol>
  <p>This is my first attempt to write this function:</p>
  <pre><code>filterOptions(
    options: { language: string; value: { parameterId: string; parameterValue: string } }[],
  ) {
    const languageIndependentKey = &quot;-&quot;;
  
    if (languageIndependentKey in options) {
      return options[languageIndependentKey];
    }
    if (this.translate.currentLang in options) {
      ...
    } else if (&quot;en&quot; in options) {
      ...
    } else {
      ...
    }
  }
  </code></pre>
  <p>This does not seem to work because I get the following error in the line <code>return options[languageIndependentKey];</code>:</p>
  <pre><code>error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.
  </code></pre>
  <p>I simply cannot figure out how to write this function without causing this error. How can I check if a specific key is present in the dictionary and return the corresponding dictionary at that key? I can't imagine that this is so difficult, but everything I tried so far did not work.</p>
  <p>For some reason, this code runs just fine:</p>
  <pre><code>for (const language in options) {
    return options[language];
  }
  </code></pre>
  <p>But directly accessing an entry like <code>options[&quot;en&quot;]</code> causes an error. Why? Is my declaration of the dictionary type incorrect, or what is going on here?</p>