SolvedDefinitelyTyped Problem defining interfaces for return document with mongoose


typescript: 2.6.1
mongoose: 4.13.3
'@\types/mongoose': 4.7.27
node: 9.1.0
OS: Mac OS 10.12.6

import mongoose = require("mongoose");
mongoose.Promise = global.Promise;
interface IUser {
  uuid: number
  email: string
  password: string
  displayName: string
mongoose.connect('mongodb://localhost:27017/test', {useMongoClient: true});

interface IUserModel extends IUser, mongoose.Document { }

const userSchema = new mongoose.Schema({
  uuid: {type:Number, required: true, index: true, unique: true},
  email: {type: String},
  password: String,
  displayName: String

const User = mongoose.model("User", userSchema);

User.insertMany([{uuid: 7, email: ""}])
  .then((d:IUserModel[]) => console.log(d));

I got error :

Error:(23, 9) TS2345:Argument of type '(d: IUserModel[]) => void' is not assignable to parameter of type '((value: Document[]) => void | PromiseLike<void>) | null | undefined'.
  Type '(d: IUserModel[]) => void' is not assignable to type '(value: Document[]) => void | PromiseLike<void>'.
    Types of parameters 'd' and 'value' are incompatible.
      Type 'Document[]' is not assignable to type 'IUserModel[]'.
        Type 'Document' is not assignable to type 'IUserModel'.
          Property 'uuid' is missing in type 'Document'.
21 Answers

✔️Accepted Answer

@maicss Can you initialize your mongoose model like this?

const User = mongoose.model<IUserModel>("User", userSchema);

Can you let me know if that works?

More Issues: