Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'catch' does not exist on type 'Promise<void>'

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?

like image 268
Nepoxx Avatar asked Oct 29 '22 21:10

Nepoxx


1 Answers

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>;
like image 180
Philippe Plantier Avatar answered Nov 11 '22 06:11

Philippe Plantier