title: why is TypeScript converting string literal union type to string when assigning in object literal?
body: <p>I love string literal union types in TypeScript.
  I came across a simple case where I was expecting the union type to be retained.</p>
  
  <p>Here is a simple version:</p>
  
  <pre class="lang-js prettyprint-override"><code>let foo = false;
  const bar = foo ? 'foo' : 'bar';
  
  const foobar = {
      bar
  }
  </code></pre>
  
  <p><code>bar</code> is correctly typed as <code>'foo' | 'bar'</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/XssIM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/XssIM.png" alt="enter image description here"></a></p>
  
  <p>But <code>foobar.bar</code> gets typed as <code>string</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/Ssi5q.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Ssi5q.png" alt="enter image description here"></a></p>
  
  <p>Just curious why.</p>
  
  <h1>Update</h1>
  
  <p>So @jcalz and @ggradnig do make good points. But then I realized my use case had an extra twist:</p>
  
  <pre class="lang-js prettyprint-override"><code>type Status = 'foo' | 'bar' | 'baz';
  let foo = false;
  const bar: Status = foo ? 'foo' : 'bar';
  
  const foobar = {
      bar
  }
  
  </code></pre>
  
  <p>Interestingly, <code>bar</code> does have a type of <code>Status</code>. However <code>foobar.bar</code> has a type of <code>'foo' | 'bar'</code> still.</p>
  
  <p>It seems that the only way to make it behave how I was expecting is to cast <code>'foo'</code> to <code>Status</code> like:</p>
  
  <pre class="lang-js prettyprint-override"><code>const bar = foo ? 'foo' as Status : 'bar';
  </code></pre>
  
  <p>In that case, the typing does work properly. I am OK with that.</p>