<p dir="auto">Building on a <a href="http://stackoverflow.com/questions/36966444/how-to-create-a-circularly-referenced-type-in-typescript/36967300#36967300" rel="nofollow">question I asked on StackOverlow</a>, I was wondering if it would be a nice addition if it would be possible to define types recursively. If I am guessing correctly this wouldn't be too difficult to implement.</p>
<p dir="auto">Example:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="type Document = [number | string | Array&lt;Document&gt;]"><pre class="notranslate"><span class="pl-k">type</span> <span class="pl-smi">Document</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-smi">number</span> <span class="pl-c1">|</span> <span class="pl-smi">string</span> <span class="pl-c1">|</span> <span class="pl-smi">Array</span><span class="pl-kos">&lt;</span><span class="pl-smi">Document</span><span class="pl-kos">&gt;</span><span class="pl-kos">]</span></pre></div>
<p dir="auto">Currently, according to the <a href="https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.8">spec</a>, only classes and interfaces are allowed to reference themselves. Is there a reason for this?</p>
<blockquote>
<p dir="auto">Classes and interfaces can reference themselves in their internal structure...</p>
</blockquote>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
interface A&lt;a&gt; {
    brand: 'a';
    nested: a;
}

interface B&lt;a&gt; {
    brand: 'b';
    nested: a;
}

type C&lt;a&gt; = A&lt;a&gt; | B&lt;a&gt;;

type D = C&lt;D&gt; | string; // &lt;-- i wish
"><pre class="notranslate"><span class="pl-k">interface</span> <span class="pl-smi">A</span><span class="pl-c1">&lt;</span><span class="pl-smi">a</span><span class="pl-c1">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-c1">brand</span>: <span class="pl-s">'a'</span><span class="pl-kos">;</span>
    <span class="pl-c1">nested</span>: <span class="pl-smi">a</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">interface</span> <span class="pl-smi">B</span><span class="pl-c1">&lt;</span><span class="pl-smi">a</span><span class="pl-c1">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-c1">brand</span>: <span class="pl-s">'b'</span><span class="pl-kos">;</span>
    <span class="pl-c1">nested</span>: <span class="pl-smi">a</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">type</span> <span class="pl-smi">C</span><span class="pl-c1">&lt;</span><span class="pl-smi">a</span><span class="pl-c1">&gt;</span> <span class="pl-c1">=</span> <span class="pl-smi">A</span><span class="pl-kos">&lt;</span><span class="pl-smi">a</span><span class="pl-kos">&gt;</span> <span class="pl-c1">|</span> <span class="pl-smi">B</span><span class="pl-kos">&lt;</span><span class="pl-smi">a</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span>

<span class="pl-k">type</span> <span class="pl-smi">D</span> <span class="pl-c1">=</span> <span class="pl-smi">C</span><span class="pl-kos">&lt;</span><span class="pl-smi">D</span><span class="pl-kos">&gt;</span> <span class="pl-c1">|</span> <span class="pl-smi">string</span><span class="pl-kos">;</span> <span class="pl-c">// &lt;-- i wish</span></pre></div>