title: How do I enforce type checking for children of a class or interface?
body: <p>I asked a question yesterday that was probably a bit too broad, so this is my attempt to break it down. Link to that question here: <a href="http://%20https://stackoverflow.com/questions/66233565/how-to-define-an-abstract-model-for-use-in-an-abstract-service" rel="nofollow noreferrer">How to define an abstract model for use in an abstract service?</a></p>
  <p>I define an abstract class <code>Model</code> with a private property <code>id</code>, a constructor, and a static method create. Then, I define an interface <code>ResponseBody</code>, which implementations of this <code>Model</code> class will extend. JSON data will come back from the server in the form of <code>DerivedClass.ResponseBody</code>, which the client will pass to <code>create</code> to form a new object derived from <code>Model</code>. Below is what I have to begin with.</p>
  <pre class="lang-js prettyprint-override"><code>export abstract class Model {
    get id() {
      return this._id;
    }
  
    constructor(private _id: number) {}
  
    static create&lt;T extends Model&gt;(body: T.ResponseBody): T {
      console.error(
        &quot;Static method create() must be defined by class implementing Model.&quot;
      );
  
      return undefined;
    }
  }
  
  export module Model {
    export interface ResponseBody {
      type?: &quot;response-body&quot;;
      id: number;
    }
  }
  </code></pre>
  <p>My issue here is defining the type of <code>body</code> parameter of <code>create</code>. I get an error: <code>'T' only refers to a type, but is being used as a namespace here.</code> I want to ensure that the type enforces that the correct values are passed to create, which will always contain an <code>id</code>, but must also contain whatever is defined in derived classes. How do I accomplish this?</p>