insertMany return type incompatible with InsertManyResult<T> (TypeScript) #13904
Closed
2 tasks done
Labels
typescript
Types or Types-test related issue / Pull Request
Milestone
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.The text was updated successfully, but these errors were encountered: