question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript classes inheritance broken after upgrade from mongoose 5.10.13 to 5.11.11

See original GitHub issue

Bug

Before upgrading to mongoose 5.11.11, the following code was working fine (I simplified it for clarity):

abstract class Route {
 constructor (readonly ObjectModel: Model<any>) {
  }

  async getAll(req: Request, res: Response) {
    query = this.ObjectModel.find()
    query.exec((err, docs) => {
      if (err) return res.status(400).json({ message: err })
      res.json(docs)
    })
  }
}

class ImageRoute extends Route {
  constructor () {
    super(Images)
  }
  this.ObjectModel.findOne({ filename: filename })
    .then((doc) => {
      if (doc) {
        doc.isValid = true
        doc.save()
          .then((savedDoc) => {
            if (savedDoc === doc) {
              res.redirect(someWhere)
            } else {
              res.status(500).json(errorMessage)
            }
          })
      }
    })
}

Where Images is a Model.

What is the current behavior?

After upgrade I get the following Typescript error, which ic occuring for all my existing Models using the same inheritance:

ERROR in xxx\src\controlers\ImageRoute.ts
./src/controlers/ImageRoute.ts
[tsl] ERROR in xxx\src\controlers\ImageRoute.ts(27,20)
      TS2345: Argument of type 'Model<ImageDocument>' is not assignable to parameter of type 'Model<Document<any>>'.
  The types returned by 'createCollection(...)' are incompatible between these types.
    Type 'Promise<Collection<ImageDocument>>' is not assignable to type 'Promise<Collection<Document<any>>>'.
      Type 'Collection<ImageDocument>' is not assignable to type 'Collection<Document<any>>'.
        Types of property 'bulkWrite' are incompatible.
          Type '{ (operations: BulkWriteOperation<ImageDocument>[], callback: MongoCallback<BulkWriteOpResultObject>): void; (operations: BulkWriteOperation<...>[], options?: CollectionBulkWriteOptions | undefined): Promise<...>; (operations: BulkWriteOperation<...>[], options: CollectionBulkWriteOptions, callback: MongoCallback<.....' is not assignable to type 
'{ (operations: BulkWriteOperation<Document<any>>[], callback: MongoCallback<BulkWriteOpResultObject>): void; (operations: BulkWriteOperation<...>[], options?: CollectionBulkWriteOptions | undefined): Promise<...>; (operations: BulkWriteOperation<...>[], options: CollectionBulkWriteOptions, callback: MongoCallback<.....'.
            Types of parameters 'operations' and 'operations' are incompatible.
              Type 'BulkWriteOperation<Document<any>>[]' is not assignable to type 'BulkWriteOperation<ImageDocument>[]'.
                Type 'BulkWriteOperation<Document<any>>' is not assignable to type 'BulkWriteOperation<ImageDocument>'.
                  Type 'BulkWriteInsertOneOperation<Document<any>>' is not assignable to type 'BulkWriteOperation<ImageDocument>'.
                    Type 'BulkWriteInsertOneOperation<Document<any>>' is not assignable to type 'BulkWriteInsertOneOperation<ImageDocument>'.
                      The types of 'insertOne.document' are incompatible between these types.
                        Type 'Pick<Document<any>, "__v" | "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | "$locals" | "$markValid" | "$op" | "$session" | "$set" | "$where" | ... 40 more ... | "validateSync"> & { ...; }' is not assignable to type 'WithId<ImageDocument>'.
                          Type 'Pick<Document<any>, "__v" | "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | "$locals" | "$markValid" | "$op" | "$session" | "$set" | "$where" | ... 40 more ... | "validateSync"> & { ...; }' is missing the following properties from type 'ImageDocument': title, description, keywords, folder, and 7 more.
 @ ./src/routes/images.ts 15:21-56
 @ ./src/api.ts 48:20-46

If the current behavior is a bug, please provide the steps to reproduce. See code above.

I’m using the following Typescript config:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "sourceMap": true,
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

What is the expected behavior? I don’t see any reason why Model<ImageDocument> and Model<Document<any>> have become incompatible.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. Before upgrade: Node v14.15.0 MongoDB 4.4.1 Community Mongoose 5.10.13 @types/mongoose 5.10.0

After upgrade: Node v14.15.0 MongoDB 4.4.1 Community Mongoose 5.11.11

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:7

github_iconTop GitHub Comments

4reactions
stalniycommented, Jan 13, 2021

This issue is related to breaking changes introduced by 5.11, when official types where provided. Typescript merges @types/mongoose and mongoose/index.d.ts together what leads to incompatibility issues in types. As a temporary workaround add post install hook in package.json:

{
  "scripts": {
    "postinstall": "rm -f ./node_modules/mongoose/index.d.ts"
  }
}

or rewrite your app to use official typings

1reaction
tipoc123commented, Feb 13, 2021

This issue is related to breaking changes introduced by 5.11, when official types where provided. Typescript merges @types/mongoose and mongoose/index.d.ts together what leads to incompatibility issues in types. As a temporary workaround add post install hook in package.json:

{
  "scripts": {
    "postinstall": "rm -f ./node_modules/mongoose/index.d.ts"
  }
}

or rewrite your app to use official typings

It works fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mongoose/History.md - UNPKG
mongoose /History.md ; 3, * fix(schema): check that schema type is an object when setting isUnderneathDocArray #10361 [vmo-khanus](https://github.com/vmo-khanus).
Read more >
New version 5.11.0 error for mongoose - Drivers & ODMs
If a Mongoose upgrade introduces breaking changes, I suggest: Reviewing the upgrade notes to see if this change is expected. Checking the GitHub ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found