title: Using Mongoose and Typescript, what type should be used for a ref field of an interface?
body: <p>I am using mongoose and Typescript, and I am wanting to know what type, or types, I should be using for a reference field, when creating an interface? Consider the following two related interfaces:</p>
  
  <pre class="lang-js prettyprint-override"><code>interface ICat {
    name: string,
    colour: string,
  }
  
  interface ICatDB extends ICat, Document {};
  
  </code></pre>
  
  <pre class="lang-js prettyprint-override"><code>interface IMouse {
    name: string,
    colour: string,
    chasedBy: /* ... */
  }
  
  interface IMouseDB extends IMouse, Document {};
  </code></pre>
  
  <p>And the schemas and models that use them:</p>
  
  <pre class="lang-js prettyprint-override"><code>let cat = new Schema({
    name: String,
    colour: String,
  });
  
  mongoose.model&lt;ICatDB&gt;('Cat', cat);
  </code></pre>
  
  <pre class="lang-js prettyprint-override"><code>let mouse = new Schema({
    name: String,
    colour: String,
    chasedBy: { type: Schema.Types.ObjectId, ref: 'Cat' }
  });
  
  mongoose.model&lt;IMouseDB&gt;('Mouse', mouse);
  
  </code></pre>
  
  <p>For the <code>chasedBy</code> field we need to consider that it can take values in three forms:</p>
  
  <ul>
  <li><code>String</code> or <code>ObjectId</code>, when passed to a <code>create()</code> method</li>
  <li><code>ObjectId</code> when returned from Mongoose</li>
  <li>Instance of <code>ICat</code> when returned from Mongoose, using <code>populate()</code></li>
  </ul>
  
  <p>Is there a way that we can specify the types the interface can support, without having to resort to using <code>any</code>?</p>
  
  <p>BTW we separated <code>IMouse</code> and <code>IMouseDB</code>, since Typescript wanted all the fields for <code>Document</code> filled out every time we created a new <code>IMouse</code> object, so this was a work around.</p>