title: How to read key from unknown typed object in TypeScript
body: <p>The following snippet won't compile with <code>typescript@4.0.2</code>.</p>
  <pre class="lang-js prettyprint-override"><code>const foo: unknown = {bar: 'baz'}
  
  if (foo &amp;&amp; typeof foo === 'object' &amp;&amp; 'bar' in foo) {
    console.log(foo.bar)
  }
  </code></pre>
  <p>The tsc error being</p>
  <pre><code>Property 'bar' does not exist on type 'object'.
  </code></pre>
  <p>How does one convince <code>tsc</code> that foo can have arbitrarily named keys without an explicit cast?</p>
  <p>Here's a playground link. <a href="https://www.typescriptlang.org/play?#code/MYewdgzgLgBAZiEAuGBXMBrMIDuYYC8MA3gEYCGATigOQUBeNAvgFAsCWcMAFAiDADIBMKAE8ADgFMQXPoQJEaIUgCtJwKDUHC6VLe3x8AlCRYwYoSCAA2kgHTWQAc16I7FSkZZMgA" rel="nofollow noreferrer">https://www.typescriptlang.org/play?#code/MYewdgzgLgBAZiEAuGBXMBrMIDuYYC8MA3gEYCGATigOQUBeNAvgFAsCWcMAFAiDADIBMKAE8ADgFMQXPoQJEaIUgCtJwKDUHC6VLe3x8AlCRYwYoSCAA2kgHTWQAc16I7FSkZZMgA</a></p>