title: Typescript: Why doesn't my code compile if a property value comes from a nested object?
body: <p>I apologize for the poor title, but I don't have the vocabulary to word it better. Let me show you my code:</p>
  <pre><code>type Foo = &quot;bar&quot; | &quot;baz&quot;;
  
  type Consistency  = {
      [K in Foo]: {
          foo: K;
          fooTemplate: `${K} in a template`;
      }
  }[Foo]
  
  // I want this to compile (and it does)
  const valid1: Consistency = {
      foo: &quot;bar&quot;,
      fooTemplate: &quot;bar in a template&quot;,
  }
  
  const valid2: Consistency = {
      foo: &quot;baz&quot;,
      fooTemplate: &quot;baz in a template&quot;,
  }
  
  export const anFoo: Foo = &quot;bar&quot;
  
  const valid3: Consistency = {
      foo: anFoo,
      fooTemplate: `${anFoo} in a template` as const
  }
  
  export interface MyObj {
    temporary: {    
      myProperty: Foo;    
    };
  }
  const state: MyObj = {
      temporary: {
          myProperty: &quot;bar&quot; 
      }
  }
  const i: Foo = state.temporary.myProperty;
  const valid4: Consistency = { // Why doesn't this one compile????
      foo: i,
      fooTemplate: `${i} in a template` as const
  }
  
  // I don't want this to compile (and it doesn't)
  const invalid1: Consistency = {
      foo: &quot;bar&quot;,
      fooTemplate: &quot;baz in a template&quot;,
  }
  
  const invalid2: Consistency = {
      foo: &quot;baz&quot;,
      fooTemplate: &quot;bar in a template&quot;,
  }
  </code></pre>
  <p><a href="https://www.typescriptlang.org/play?#code/C4TwDgpgBAYg9nKBeKAiARgQwE6qgHzSwC9UBuAKAtEigGE4A7AZwEtngJGBjEKZKAG8K-fgG0A0lFaNYCALoAuISNFqAZgmUTKajQgAqEALZgANpk7KABgBJBEgL7TZmKJ1MXO13aMcVHMXg4eSoAejCoAEkoAHdMRmB3AAt2d0RuOFNWM2gACgSAE2kkwrgIZgBKCkyWJIA3TDNWQoBGZQYWdk4ePhRhfThlDBxUABpVfk04I09LCGGsbBcoNw9zefGAqlqOKEbmwoAmDqY2Di5eAQHRacXMUgnB2Y2rIgeVtZNXiC3-CggAA8wHBsEldkkEsFlMEBCNcDszg0mi0AMynLoXXrXSZQO6rRjBJ63QzfLwLKB2QRQhDOGSrdxk+bWVbMKAQ7YA4GgpIyTjYdSYbjQACyIAA8ugAFYqfjrUE4EDKQRqXHGEAABWwcEgYKVcjgZFV-EclH8EKgHHmyjFkpl-Vx8uwiuVuLU6q1OogevuuCguP85qR0hhCAEVs4ADonYrIx7tbrQJQLQcWgAWDHnHpXfpQCJQADqyT4ZQqjAA5ElgKk2UxoJlsrkAPwtpu4-GsYlTUlzN5U1h01yM3sQFmYNkc-75mJlCtJeKJFJpYAZLJgHL5IolKCl5hz6oWmSptqZ7qXPqyklDd64Lt4ns-e7ET7Dn5-RF1FzHk70M5n7EOoMT5bM8TJvPCL7rOSfxAA" rel="nofollow noreferrer">You can play with this here.</a> This all works the way I expect accept for <code>valid4</code>. For some reason, <code>valid4</code> doesn't compile, and I want it to.  Why isn't it compiling and how do I make it compile?</p>
  <hr />
  <p>The answer doesn't explicitly say this so I will: There's no solution to this problem.</p>