[X] I have searched existing issues to ensure the bug has not already been reported
Mongoose version
7.5.3
Node.js version
v18.13
MongoDB server version
7.x
Typescript version (if applicable)
No response
Description
While migrating a NestJs/TypeScript project from mongoose v6.10.3 to v7.5.3, I have faced a problem with the return type of Model.insertMany method. I have a method handling creating many records and then reporting the errors/duplicated/created documents, where I directly use InsertManyResult type from mongoose, and the typed assignment like below is crucial.
... Type 'Error' is not assignable to type 'CastError | ValidatorError'.
Type 'MongooseError' is missing the following properties from type 'ValidatorError': properties, kind, path, valuets(2322)
Similar code worked in v6.10.3, but this commit has introduced a new overload for insertMany which is as follows:
Prerequisites
Mongoose version
7.5.3
Node.js version
v18.13
MongoDB server version
7.x
Typescript version (if applicable)
No response
Description
While migrating a NestJs/TypeScript project from mongoose v6.10.3 to v7.5.3, I have faced a problem with the return type of
Model.insertMany
method. I have a method handling creating many records and then reporting the errors/duplicated/created documents, where I directly useInsertManyResult
type frommongoose
, and the typed assignment like below is crucial.The compile error boils down to:
Similar code worked in v6.10.3, but this commit has introduced a new overload for insertMany which is as follows:
while the exported InsertManyResult type looks like this:
And the error is caused by
validationErrors: Error[]
being incompatible withvalidationErrors?: Array<Error.CastError | Error.ValidatorError>
.While I could fix the issue by adding a custom type
I would prefer to use the library types.
Steps to Reproduce
Link to the sandbox with demo: https://codesandbox.io/s/typescript-playground-forked-9mjzg5?file=/src/index.ts
Expected Behavior
The code compiles and
InsertManyResult<T>
type actually represents what 'insertMany' returns.