title: Describe typescript object where some keys are known
body: <p>How can I define a type for my typescript object where some of the keys are known, others are either unknown or defined as a separate type.</p>
  <p>Take the following object:</p>
  <pre><code>{
    foo: 1,
    bar: &quot;abc&quot;,
    baz: {
      foobar: 2
    },
    foobaz: {
      foobar: 1
    }
  }
  </code></pre>
  <p>I know the object will always have the properties <code>foo</code> and <code>bar</code> as defined in this type:</p>
  <pre><code>type MyObject = {
    foo: number;
    bar: string;
  }
  </code></pre>
  <p>But I know that the object <em>may</em> have other properties and if they do, they will always have the same structure of an object with a property foobar and a value of the type number.</p>
  <p>How do I defined this type?</p>
  <p>I tried:</p>
  <pre><code>type MyObject = {
    foo: number;
    bar: string;
    [key: string]?: {
      foobar: number;
    }
  }
  </code></pre>
  <p>But it appears you can't declare a generic key like that when you have explicit keys in the same type.</p>
  <p>I actually know the possible values of the &quot;unknown&quot; properties. So I tried this:</p>
  <pre><code>type MyList = 'baz'|'foobaz';
  
  type MyObject = {
    foo: number;
    bar: string;
    [key in MyList]?: {
      foobar: number;
    }
  }
  </code></pre>
  <p>The <code>[key in MyList]</code> part works alone, but as soon as I combine it with the explicit properties I get an error: &quot;'MyList' only refers to a type, but is being used as a value here&quot;. I tried the same with an Enum and got the same result.</p>