title: TypeScript: type for an object with only one key (no union type allowed as a key)
body: <p>I'm hoping to define a <code>type</code> of objects that can have exactly one key.</p>
  
  <p>Here is an attempt:</p>
  
  <pre><code>type OneKey&lt;K extends string&gt; = Record&lt;K, any&gt;
  </code></pre>
  
  <p>Unfortunately, this doesn't quite work because a variable can have a union type:</p>
  
  <pre><code>type OneKey&lt;K extends string&gt; = Record&lt;K, any&gt;
  
  declare function create&lt;
      K extends string,
      T extends OneKey&lt;K&gt;[K]
  &gt;(s: K): OneKey&lt;K&gt;
  
  const a = "a";
  const res = create(a);
  
  
  // Good
  const check: typeof res = { a: 1, b: 2 }
  //                                ~~ Error, object may only specify known properties
  
  declare const many: "a" | "b";
  const res2 = create(many);
  
  
  // **Bad**: I only want one key
  const check2: typeof res2 = { a: 1, b: 2 }; // No error
  declare const x: "k1" | "k2"
  </code></pre>