<p dir="auto">I ran into a scenario yesterday that surprised me a bit. I was using an object literal to store some unique keys that I later extracted with a for-in loop. The keys were all numbers, but I quickly ran into a problem at runtime because i had forgotten that JavaScript coerces number type keys to strings for objects.</p>
<p dir="auto">To wit, given the coercion of keys to strings, I was surprised that this type checked:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var ids: Array&lt;number&gt; = [];
const myCollection = {
    0: true,
    1: true
};

for (var key in myCollection) {
    ids.push(key);
}

console.log(ids); // =&gt; [&quot;0&quot;, &quot;1&quot;]"><pre class="notranslate"><span class="pl-k">var</span> <span class="pl-s1">ids</span>: <span class="pl-smi">Array</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">;</span>
<span class="pl-k">const</span> <span class="pl-s1">myCollection</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span>
    <span class="pl-c1">0</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-c1">1</span>: <span class="pl-c1">true</span>
<span class="pl-kos">}</span><span class="pl-kos">;</span>

<span class="pl-k">for</span> <span class="pl-kos">(</span><span class="pl-k">var</span> <span class="pl-s1">key</span> <span class="pl-k">in</span> <span class="pl-s1">myCollection</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-s1">ids</span><span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-s1">key</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s1">ids</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// =&gt; ["0", "1"]</span></pre></div>
<p dir="auto">Is this expected behavior, or should this throw a compile time warning?</p>
<p dir="auto">(TSC Version 1.7.5)</p>
<p dir="auto">In the following statement,</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="for (let subscription in this._subscriptions) {
    // Do something with subscription
}"><pre class="notranslate"><code class="notranslate">for (let subscription in this._subscriptions) {
    // Do something with subscription
}
</code></pre></div>
<p dir="auto">The variable <code class="notranslate">subscription</code> is considered type <code class="notranslate">any</code>. Although I'm compiling with <code class="notranslate">noImplicitAny</code> as true, no error is given. Based on the for spec, it seems as if <code class="notranslate">subscription</code> should implicitly be considered <code class="notranslate">string</code>.</p>
<p dir="auto">Because of this, it's possible to compile with typo functions that will cause errors at runtime.</p>