属性“catch”在types'Promise <void>'上不存在

我正在用打字机使用mongoose和蓝鸟。 Mongoose设置为返回Bluebird诺言,但是我不知道如何“告诉”TypeScript。

例如,我有一个Message Mongoose模型,如果我尝试执行以下操作:

 new Message(messageContent) .save() .then(() => {...}) .catch(next); 

TypeScript抱怨Property 'catch' does not exist on type 'Promise<void>'. 因为它认为.save() (或任何其他Mongoose方法返回Promise)会返回一个“常规”Promise(实际上没有.catch()方法),而不是蓝鸟许诺。

我怎样才能改变Mongoose方法的返回types,使TypeScript知道它返回一个蓝鸟的承诺?

根据DefinitelyTyped的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>; */ 

所以,你的主要.d.ts文件将如下所示:

 /// <reference path="globals/bluebird/index.d.ts" /> /// <reference path="globals/mongoose/index.d.ts" /> type MongoosePromise<T> = bluebird.Promise<T>; 

我已经完成了这个扩展mongoose模块:

 declare module "mongoose" { import Bluebird = require("bluebird"); type MongoosePromise<T> = Bluebird<T>; } 

在这里看到我的答案: 与蓝鸟和打字稿mongoose承诺