francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
105 stars 25 forks source link

Type error: Argument of type 'Schema<Document<any, any, any>, Model<any, any, any>, undefined, any>' is not assignable to parameter of type 'Schema<InvoiceDocument, Model<InvoiceDocument, InvoiceQueries, {}>, undefined, {}>'. #62

Closed shahidcodes closed 3 years ago

shahidcodes commented 3 years ago
Argument of type 'Schema<Document<any, any, any>, Model<any, any, any>, undefined, any>' is not assignable to parameter of type 'Schema<InvoiceDocument, Model<InvoiceDocument, InvoiceQueries, {}>, undefined, {}>'.
  Types of property 'plugin' are incompatible.
    Type '(fn: (schema: Schema<Document<any, any, any>, Model<Document<any, any, any>, {}, {}>, undefined, {}>, opts?: any) => void, opts?: any) => Schema<...>' is not assignable to type '(fn: (schema: Schema<InvoiceDocument, Model<InvoiceDocument, {}, {}>, undefined, {}>, opts?: any) => void, opts?: any) => Schema<InvoiceDocument, Model<...>, undefined, {}>'.
      Types of parameters 'fn' and 'fn' are incompatible.
        Types of parameters 'schema' and 'schema' are incompatible.
          Type 'Schema<Document<any, any, any>, Model<Document<any, any, any>, {}, {}>, undefined, {}>' is not assignable to type 'Schema<InvoiceDocument, Model<InvoiceDocument, {}, {}>, undefined, {}>'.
            Types of property 'static' are incompatible.
              Type '{ (name: string, fn: (this: Model<Document<any, any, any>, {}, {}>, ...args: any[]) => any): Schema<Document<any, any, any>, Model<Document<any, any, any>, {}, {}>, undefined, {}>; (obj: { ...; }): Schema<...>; }' is not assignable to type '{ (name: string, fn: (this: Model<InvoiceDocument, {}, {}>, ...args: any[]) => any): Schema<InvoiceDocument, Model<InvoiceDocument, {}, {}>, undefined, {}>; (obj: { ...; }): Schema<...>; }'.
                Types of parameters 'fn' and 'fn' are incompatible.
                  The 'this' types of each signature are incompatible.
                    Type 'Model<Document<any, any, any>, {}, {}>' is not assignable to type 'Model<InvoiceDocument, {}, {}>'.
                      The types returned by 'count(...).$where(...).exec()' are incompatible between these types.
                        Type 'Promise<Document<any, any, any>[]>' is not assignable to type 'Promise<InvoiceDocument[]>'.
                          Type 'Document<any, any, any>[]' is not assignable to type 'InvoiceDocument[]

invoice.ts


import mongoose, { Schema } from 'mongoose';
import { InvoiceDocument, InvoiceModel } from './types/mongoose.gen';

const schema = new Schema({
  _id: Number,
  invoiceNumber: {
    type: String,
    required: true,
  },
  user: {
    type: Number,
    ref: 'User',
  },
  createdAt: Date,
  updatedAt: Date,
  status: {
    type: String,
    enum: ['created', 'paid', 'deleted'],
  },
  transactionId: String,
  updatedRemote: Boolean,
  items: [
    {
      description: String,
      quantity: String,
      unit: Number,
      amount: Number,
      tax: Number,
      from: Date,
      to: Date,
      _id: Number,
    },
  ],
  totalAmount: Number,
});

export const Invoices = mongoose.model<InvoiceDocument, InvoiceModel>(
  'Invoice',
  schema
);

package.json

    "mongoose-tsgen": "^8.0.2",
    "mongoose": "^5.12.2",
francescov1 commented 3 years ago

Hey @shahidcodes, sorry I've been super busy lately, will try to check this out over the next few days!

francescov1 commented 3 years ago

Hi @shahidcodes, I'm not able to reproduce your issue since it seems to be coming from some external document (based on your error message, I assume you have an array of InvoiceDocument on another schema, this seems to be causing the problem). Can you provide a more complete example of the issue?

shahidcodes commented 3 years ago

Hi @francescov1 , Initially as a workaround I removed the InvoiceModel from mongoose.model call but now I am trying to reproduce it seems fine. Thanks for the support and sorry for late reply