title: Typing and validating an array of objects of unknown shape
body: <p>I spent quite some time on this and I would appreciate some help. I want a component (a function) to accept an array of objects while also validating the properties of the objects.</p>
  <p>Interfaces and data:</p>
  <pre><code>interface ObjectWithId {
    id: string | number;
  }
  
  interface TableMeta&lt;T extends ObjectWithId, K extends PropertyKey = keyof T&gt; {
    data: T[];
    searchKey: K;
    onClick?: (item: T) =&gt; void;
  }
  
  interface Vegetable {
    id: number,
    label: string,
  }
  
  interface Fruit {
    id: number, 
    name: string,
  }
  
  const vegetableMeta: TableMeta&lt;Vegetable&gt; = {
    data: [],
    searchKey: 'label', // this only allows 'label' or 'id' 
  }
  
  const fruitMeta: TableMeta&lt;Fruit&gt; = {
    data: [],
    searchKey: 'name', // this only allows 'name' or 'id' 
    onClick: (item) =&gt; {item.id} // ✔️ has correct item type &lt;---------------
  }
  
  const metas = [vegetableMeta, fruitMeta];
  </code></pre>
  <p>A component (a function for simplicity):</p>
  <pre><code>const metaParser = (metas: TableMeta&lt;{id: number | string}, PropertyKey&gt;[]) =&gt; {
    const id = metas[0].data[0].id; // should be `number | string`
  }
  
  metaParser(metas); // ❌ Type 'ObjectWithId' is not assignable to type 'Vegetable'
  </code></pre>
  <p><strong>The shape of objects in the array is unknown in advance</strong></p>
  <p>Any ideas on how to make it work?</p>
  <p><a href="https://www.typescriptlang.org/play?ssl=32&amp;ssc=42&amp;pln=1&amp;pc=1#code/JYOwLgpgTgZghgYwgAgPICMBWEFgOrBgAWAkgCbIDeAUMssGQFzIDOYUoA5sgD7IgBXALbpoAbmoBfatVCRYiFABU46ADYQAshDBwAPEuQQAHpBBkWaLDnyFSZADTIA0kdMRzlgApQA9gAdoMABPZwhg5ABeZABrcN8YZCUAPipaZDI4XWYlAG0AXQk6Fgg4KAQiMODmZyLkXxAAYTVgBBiAfmYACkIIIRyASijUgDdfBglpWXBoeCRkADUITh1VDTS6BmZBEWgHdLVVCDVmNg4QTn2puVnFZAAxKAFCDfomfmFRKCd0kDghCCndhcK4yBANNjIEbLVbqLSrHJreG6PRLFa6OGpaI0OiZbLIAr7YqlcqVcLMADkh1EagpTgA9PTkMRgJYGmoInA1GpfAB3SxUo60+pQZAUhgU5CAXg3ALI7UjBELAyBgT0I2nxKjh6v0j2eYCxrzxcGYhPSJTKFSqlL+ALpyEZzKIrPqIA5yC5PP5YptEElvlF4rIktl6QazVaMW6vSEQ0iqUo0YAdAxJPamYAUckA8H-IIhwSzgqBQGz0SBCZnBQLIPQAWlrdfrDfl1HBIEhAN0lmiuWh6KR2qcKr12sKMmbiuQ7bgXjKJVF0S6k5YiK1qz0GGwuAIxHITh8ASCoXCyQKsfj6RbkIYUQnqxYuQADPlE0aH0+JmnWERfAI1BRRMgAAMdi+XhWGBC4AKbSdpygWcF1vAYxCAA" rel="nofollow noreferrer">TS Playground</a></p>