I'm using Mongoose and Bluebird with typescript. Mongoose is setup to return Bluebird promises, but I don't know how to "tell" TypeScript about it.
For instance, I have a Message
Mongoose model, if I try to do the following:
new Message(messageContent)
.save()
.then(() => {...})
.catch(next);
TypeScript complains that Property 'catch' does not exist on type 'Promise<void>'.
because it thinks that .save()
(or any other Mongoose methods returning a Promise) returns a 'regular' Promise (which indeed doesn't have the .catch()
method) instead of a Bluebird promise.
How can I change the return type of Mongoose methods so that TypeScript knows it is returning a Bluebird promise?
According to DefinitelyTyped's mongoose.d.ts
/**
* To assign your own promise library:
*
* 1. Include this somewhere in your code:
* mongoose.Promise = YOUR_PROMISE;
*
* 2. Include this somewhere in your main .d.ts file:
* type MongoosePromise<T> = YOUR_PROMISE<T>;
*/
So, your main .d.ts file would look like the following:
/// <reference path="globals/bluebird/index.d.ts" />
/// <reference path="globals/mongoose/index.d.ts" />
type MongoosePromise<T> = bluebird.Promise<T>;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With