TS2351:不能对类型缺少调用或构造签名的表达式使用“new”。猫鼬

Pao*_*o P 2 mongoose typescript-typings

不知道我做错了什么,但我有这个类/模块:

import * as Mongoose from 'mongoose';
import * as _ from 'lodash';
import * as Promise from 'bluebird';
import { db } from '../lib/database';

let mongoose = Mongoose;

mongoose.Promise = Promise;


export class BaseModel  {

    constructor(schemaObj: {}, modelName: string, statics?: {}) {
      let Schema = new mongoose.Schema(schemaObj)

      Schema.statics.list = function () {
        return this.find()
      }

      Schema.statics.save = function (dataObj) {
        return this.save(dataObj)
      }

      if (statics) {
        for (let key in statics) {
          Schema.statics[key] = statics[key];
        }
      }
      
      return mongoose.model(modelName, Schema);
    }
    
}

let schema = {
  title:  String,
}

let Message = new BaseModel(schema, 'message');

let j = new Message({title: 'Hello World'}).save().then(function (res) {
  console.log(res);
})
Run Code Online (Sandbox Code Playgroud)

一切实际上都有效,但 TypeScript 抱怨:

TS2351:不能对类型缺少调用或构造签名的表达式使用“new”。

我知道我错过了一些东西,但我不知道是什么。

nad*_*son 5

* as导入 mongoose 时尝试删除。所以它会变成 import Mongoose from 'mongoose';