Strongly typed models with Mongoose and TypeScript

Tom Nagle
4 min readFeb 11, 2019

Mongoose provides a layer of abstraction over MongoDB that makes validation, casting & business logic boilerplate more enjoyable. Creating your models with a TypeScript interface extends these benefits by creating a strongly typed model that increases developer confidence, development speed and reduces bugs.

How to create strongly typed Mongoose models with TypeScript

There are two main ways to make your models strongly typed, Typegoose & and custom interfaces. This post will focus on custom interfaces. Typegoose adds too much abstraction to really know what’s going on.

Assumptions

  • TypeScript installed and working
  • @types/mongoose installed & working
  • Jest & mongodb-memory-server installed & working

You can see an example repository that has everything you need to get started here: https://github.com/tomanagle/Mongoose-TypeScript-example

--

--

Tom Nagle

I am a full stack JavaScript developer, living in Melbourne, Australia. My preferred stack is Mongoose, TypeScript, Node.js, React & GraphQL.