If you’re using Mongoose with TypeScript on your NodeJS project with RxJS you’ll probably want to avoid using promises as much as possible. Here’s an extension method on the Mongoose.

This will allow you to do something like this:

Currently if you want to use mongoose with TypeScript then you’ll have to resort to something like this

This is quite verbose. So let’s create an extension method around it.

# Make Sure You Have The Dependencies

Make sure you have your typings by running these commands

# Create an Extension Methods File

You’ll want to create an extensions file named something like extensions.ts. I put

Important: you’ll want to require this file immediately once at the beginning of your main execution file. For example, if you’re using this in ExpressJS and you have an app.js or server.js file, then you’ll want to require it like so:

## Explainer

mongoose Is a popular library but it’s structure is quite nasty in terms of modern JavaScript patterns. This is a better exercise of creating extension methods in TypeScript.
If you take a look at the index.d.ts in the mongoose typings file, you’ll see that that queries are primarily subclassed from a type called mquery. The generic DocumentQuery<T> extends from mquery so we need to create an extension on both interfaces

The actual implemention that is mquery so we need to create the prototype method from that and not DocumentQuery<T>.

Mayhaps one day in TypeScript, creating extensions will be as easy as Swift or C#. Right now there’s a lot of training that you have to do with TypeScript and the underlying JavaScript context.