Awesome Open Source
Awesome Open Source


(These badges are from typegoose:master)
Node.js Tests Coverage Status npm

Define Mongoose models using TypeScript classes

Basic usage

import { prop, getModelForClass } from '@typegoose/typegoose';
import * as mongoose from 'mongoose';

class User {
  public name?: string;

  @prop({ type: () => [String] })
  public jobs?: string[];

const UserModel = getModelForClass(User); // UserModel is a regular Mongoose Model with correct types

(async () => {
  await mongoose.connect('mongodb://localhost:27017/', { useNewUrlParser: true, useUnifiedTopology: true, dbName: "test" });

  const { _id: id } = await UserModel.create({ name: 'JohnDoe', jobs: ['Cleaner'] } as User); // an "as" assertion, to have types for all properties
  const user = await UserModel.findById(id).exec();

  console.log(user); // prints { _id: 59218f686409d670a97e53e0, name: 'JohnDoe', __v: 0 }


A common problem when using Mongoose with TypeScript is that you have to define both the Mongoose model and the TypeScript interface. If the model changes, you also have to keep the TypeScript interface file in sync or the TypeScript interface would not represent the real data structure of the model.

Typegoose aims to solve this problem by defining only a TypeScript interface (class), which needs to be enhanced with special Typegoose decorators (like @prop).

Under the hood it uses the Reflect & reflect-metadata API to retrieve the types of the properties, so redundancy can be significantly reduced.

Instead of writing this:

// This is an representation of how typegoose's compile output would look like
interface Car {
  model?: string;

interface Job {
  title?: string;
  position?: string;

interface User {
  name?: string;
  age!: number;
  preferences?: string[];
  mainJob?: Job;
  jobs?: Job[];
  mainCar?: Car | string;
  cars?: (Car | string)[];

const JobSchema = new mongoose.Schema({
  title: String;
  position: String;

const CarModel = mongoose.model('Car', {
  model: string,

const UserModel = mongoose.model('User', {
  name: { type: String },
  age: { type: Number, required: true },
  preferences: [{ type: String }],
  mainJob: { type: JobSchema },
  jobs: [{ type: JobSchema }],
  mainCar: { type: Schema.Types.ObjectId, ref: 'Car' },
  cars: [{ type: Schema.Types.ObjectId, ref: 'Car' }],

You can just write this:

class Job {
  public title?: string;

  public position?: string;

class Car {
  public model?: string;

class User {
  public name?: string;

  @prop({ required: true })
  public age!: number; // This is an single Primitive

  @prop({ type: () => [String] })
  public preferences?: string[]; // This is an Primitive Array

  public mainJob?: Job; // This is an single SubDocument

  @prop({ type: () => Job })
  public jobs?: Job[]; // This is an SubDocument Array

  @prop({ ref: () => Car })
  public mainCar?: Ref<Car>; // This is an single Reference

  @prop({ ref: () => Car })
  public cars?: Ref<Car>[]; // This is an Reference Array

Extra Examples


  • TypeScript ^3.9 (since 7.1)
  • NodeJS ^10.15.0
  • Mongoose 5.10.18 (look here for why this version)
  • @types/mongoose 5.10.x
  • experimentalDecorators and emitDecoratorMetadata must be enabled in tsconfig.json
  • tsconfig option target being ES6

Note: it is recommended to not use babel see here why


npm i -s @typegoose/typegoose # install typegoose itself

npm i -s mongoose # install peer-dependencie mongoose
npm i -D @types/mongoose # install all types for mongoose - this is required for typegoose to work in typescript


npm i -D
npm test


This Project should comply with Semver. It uses the Major.Minor.Fix standard (or in NPM terms, Major.Minor.Patch).

Join Our Discord Server

To ask questions or just talk with us, join our Discord Server.


Known Issues

Here are the known-issues


Here is the FAQ


  • Please don't add +1 or similar comments to issues. Use the reactions instead.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
typescript (10,336
nodejs (3,625
mongodb (769
js (509
mongoose (163
model (106
models (63
db (53
ts (44
odm (26

Find Open Source By Browsing 7,000 Topics Across 59 Categories