title: How should I define interfaces of documents when using Typescript and Mongodb?
body: <p>Consider a simple user collection: </p>
  
  <pre><code>// db.ts
  export interface User {
        _id: mongodb.ObjectId;
        username: string;
        password: string;
        somethingElse: string;
  }
  
  // user.ts
  import {User} from "../db"
  router.get("/:id", async (req, res) =&gt; {
      const id = req.params.id;
      // user._id is a mongodb.Object.
      const user: User = await db.getUser(id);
  
      res.send(user);
  });
  
  // index.ts
  // code that will runs on browser
  import {User} from "../../db"
  $.get('/user/...').done((user: User) =&gt; {
      // user._id is string.
      console.log(user._id);
  });
  </code></pre>
  
  <p>It works perfectly until I want to use this interface in client codes. Because the <code>_id</code> of user becomes a hex string when tranmitted as json from server. If I set <code>_id</code> to be <code>mongodb.ObjectId | string</code>, the behavior gets wierd.
  <a href="https://i.stack.imgur.com/FmNmW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FmNmW.png" alt="enter image description here"></a></p>