title: Typescript base class reference subclass
body: <p>I am trying to create a tree-like structure with inheritance like the following:</p>
  
  <p><code>base.ts</code>:</p>
  
  <pre><code>export class BaseClass {
      children: BaseClass[] = [];
  
      get subclassChildren(): SubClass[] {
          return this.children.filter((child): child is SubClass =&gt; child instanceof SubClass);
      }
  }
  </code></pre>
  
  <p><code>sub.ts</code>:</p>
  
  <pre><code>import { BaseClass } from './base';
  export class SubClass extends BaseClass {}
  </code></pre>
  
  <p>This wont work because the base class cannot find <code>SubClass</code>:</p>
  
  <blockquote>
  <pre><code>TSError: ⨯ Unable to compile TypeScript:
  base.ts(4,26): error TS2304: Cannot find name 'SubClass'.
  base.ts(5,49): error TS2304: Cannot find name 'SubClass'.
  base.ts(5,78): error TS2304: Cannot find name 'SubClass'.
  </code></pre>
  </blockquote>
  
  <p>Not unexpected but if I try to import the subclass in <code>base.ts</code> it wont work either as there are circular references to each other:</p>
  
  <blockquote>
  <pre><code>TypeError: Object prototype may only be an Object or null: undefined
  </code></pre>
  </blockquote>
  
  <p>Is there a way to forward declare <code>SubClass</code> or some other way to get this code working?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p><code>tsconfig.json</code> (generated by <code>tsc --init</code>):</p>
  
  <pre><code>{
    "compilerOptions": {
      "target": "es5",
      "module": "commonjs",
      "strict": true,
      "esModuleInterop": true
    }
  }
  </code></pre>