New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
types(documentarray): typed DocumentArray constructor parameter #13089
Conversation
test/types/docArray.test.ts
Outdated
expectError(function getTestRouteData(): Route { | ||
return { | ||
points: new Types.DocumentArray([ | ||
{ name: 'Test' } // "location" is missing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The major problem with this approach is that you'll still get an error even if location
has a default.
Even if we work around the defaults issue, still doesn't handle the case of custom getters, or properties that are set in middleware (e.g. timestamps).
types/types.d.ts
Outdated
@@ -61,7 +61,7 @@ declare module 'mongoose' { | |||
|
|||
class DocumentArray<T> extends Types.Array<T extends Types.Subdocument ? T : Types.Subdocument<InferId<T>> & T> { | |||
/** DocumentArray constructor */ | |||
constructor(values: any[]); | |||
constructor(values: (T & AnyObject)[]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AnyObject is realistically the best we can do here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, for consistency with push, let's do AnyKeys<T> & AnyObject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍
Changed
DocumentArray
constructor parameter type fromany[]
to(T & AnyObject)[]
.closes #13087