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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 45 additions & 1 deletion test/types/docArray.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Document, Types } from 'mongoose';
import { Schema, model, Types, InferSchemaType } from 'mongoose';
import { expectError, expectType } from 'tsd';

async function gh10293() {
Expand All @@ -24,3 +24,47 @@ async function gh10293() {
return test.arrayOfArray; // <-- error here if the issue persisted
};
}

function gh13087() {
interface Book {
author: {
name: string;
};
}

expectError(new Types.DocumentArray<Book>([1, 2, 3]));

const locationSchema = new Schema(
{
type: {
required: true,
type: String,
enum: ['Point']
},
coordinates: {
required: true,
type: [Number]
}
},
{ _id: false }
);

const pointSchema = new Schema({
name: { required: true, type: String },
location: { required: true, type: String }
});

const routeSchema = new Schema({
points: { type: [pointSchema] }
});

type Route = InferSchemaType<typeof routeSchema>;

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).

])
};
});
}
2 changes: 1 addition & 1 deletion types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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


isMongooseDocumentArray: true;

Expand Down