title: What type declaration to use for a generic T that may or may not be hierarchical?
body: <p>I'm extending a React table (creatively called "Table") that takes a generic data array, along with some column-config data, and renders a table. The declaration looks like this:</p>
  
  <pre><code>export class Table&lt;T&gt; extends React.Component&lt;TableProps&lt;T&gt;, any&gt;
  </code></pre>
  
  <p>The non-optional parts of TableProps:</p>
  
  <pre><code>export interface TableProps&lt;T&gt; {
      columnConfigs: TableColumnConfig&lt;T&gt;;
      tableStore: TableStore&lt;T&gt;;
  }
  </code></pre>
  
  <p>And TableStore has only one required property:</p>
  
  <pre><code>export class TableStore&lt;T&gt; {
      public data: T[];
  }
  </code></pre>
  
  <p>TableColumnConfigs isn't terribly important, since the T is only used in an optional property:</p>
  
  <pre><code>export interface TableColumnConfigs&lt;T&gt; {
      render?: (data: T) =&gt; JSX.Element
  }
  </code></pre>
  
  <p>This &lt;Table&gt; has been used several places already, and I'm adding a feature to it: optional hierarchical data, so we can stop using <a href="https://demos.telerik.com/kendo-ui/treelist/index" rel="nofollow noreferrer">Kendo's TreeList</a> (which is very, very featureful, but a bit slow to load in our environment).</p>
  
  <p><strong>My problem:</strong> I want to declare that T, the data type used in the table and supplied by someone outside the class, <em>may</em> have a particular property that makes the type have recursive children, but is not required to.</p>
  
  <p>I've tried this:</p>
  
  <pre><code>export interface ChildData&lt;T&gt; {
      children?: (T &amp; ChildData&lt;T&gt;)[]
  }
  export class Table&lt;T extends ChildData&lt;T&gt;&gt; extends React.Component&lt;TableProps&lt;T&gt;, any&gt; ...
  </code></pre>
  
  <p>This doesn't work, because that external class doesn't define a property called "children".</p>
  
  <p>The problem is that TypeScript 2.8 doesn't like it: when I actually use Table somewhere, it whines that T isn't implementing ChildData. (I'm compiling using TS 2.3 at the moment, but I'm gonna upgrade past 2.4, at some point, real soon, I swear. Eventually. At any rate, I definitely don't want to be <em>blocked</em> from upgrading. I know it doesn't work on 2.8 because my IDE is running Intellisense using 2.8, but I'm running the 2.3 compiler.)</p>
  
  <p>I've also tried declaring using union and intersection types, but they simply don't compile, even under TS 2.3:</p>
  
  <pre><code>export class Table&lt;T &amp; ChildData&lt;T&gt;&gt; extends React.Component&lt;TableProps&lt;T&gt;, any&gt; ...
  export class Table&lt;T | ChildData&lt;T&gt;&gt; extends React.Component&lt;TableProps&lt;T&gt;, any&gt; ...
  </code></pre>
  
  <p>Is there some way to say "I have a generic parameter T, that may implement a ChildData, but isn't required to?" My &lt;Table&gt; component is already being used in several places elsewhere, and it's going to be used by people that aren't me. I don't want to require consumers of Table to also extend their T types by a "children" property that they aren't using (since the hierarchical feature of the table is entirely optional).</p>