Typescript classes inheritance broken after upgrade from mongoose 5.10.13 to 5.11.11
See original GitHub issueBug
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:
- Created 3 years ago
- Reactions:8
- Comments:7
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:
or rewrite your app to use official typings
It works fine.