title: How to properly type array of object IDs in mongoose with Typescript
body: <p>I have a Post Model:</p>
  <pre><code>const PostSchema = new Schema&lt;IPost&gt;(
    {
      // ...
      likes: [{ type: Schema.Types.ObjectId, ref: &quot;User&quot; }],
      // ...
    }
  )
  
  export default model&lt;IPost&gt;(&quot;Post&quot;, PostSchema)
  </code></pre>
  <pre><code>export interface IPost {
    // ...
    likes: ObjectId[]
    // ...
  }
  
  export interface IPostDocument extends Document, IPost {}
  </code></pre>
  <p>And I'm trying to toggle a user like:</p>
  <pre><code>export const toggleLike: TController = async (req, res, next) =&gt; {
    const user = req.user as IUserDocument;
    const userId = user._id;
    const postId = req.params.postId;
    try {
      const disliked = await PostModel.findOneAndUpdate(
        { _id: postId, likes: userId },
        { $pull: { likes: userId } }
      ); // works with no problem
      if (disliked)
        res.json({ message: `User ${userId} disliked post ${postId}` });
      else {
        const liked = await PostModel.findOneAndUpdate(
          { _id: postId },
          { $push: { likes: userId } }
        ); // the $push throws an error &quot;Type instantiation is excessively deep and possibly infinite.&quot;
        if (liked) res.json({ message: `User ${userId} liked post ${postId}` });
        else return next(createError(404, &quot;Post not found&quot;));
      }
    } catch (error) {
      next(createError(500, error as Error));
    }
  };
  </code></pre>
  <p>The mongo $push operator is throwing an error &quot;Type instantiation is excessively deep and possibly infinite.&quot;</p>
  <p>I doubt it helps but the description of the error is:</p>
  <p><em>(property) likes?: _AllowStringsForIds&lt;(((((((((((... | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[]&gt; | ArrayOperator&lt;(_AllowStringsForIds&lt;(((((((((((... | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[])[] | any[]&gt; | undefined)[]&gt; | undefined</em></p>
  <p>Any idea what's happening?</p>