Skip to content
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

Merged
merged 3 commits into from Mar 13, 2023

Conversation

lpizzinidev
Copy link
Contributor

Changed DocumentArray constructor parameter type from any[] to (T & AnyObject)[].

closes #13087

expectError(function getTestRouteData(): Route {
return {
points: new Types.DocumentArray([
{ name: 'Test' } // "location" is missing
Copy link
Collaborator

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)[]);
Copy link
Collaborator

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.

Copy link
Collaborator

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

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@vkarpov15 vkarpov15 added this to the 7.0.2 milestone Mar 13, 2023
@vkarpov15 vkarpov15 merged commit 791ab3e into Automattic:master Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DocumentArray constructor accepts array of any type what can cause runtime error
2 participants