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: pass DocType down to subdocuments so HydratedSingleSubdocument and HydratedArraySubdocument toObject() returns correct type #14612

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion test/types/subdocuments.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Schema, model, Model, Document, Types } from 'mongoose';
import {
Schema,
model,
Model,
Document,
Types,
HydratedArraySubdocument,
HydratedSingleSubdocument
} from 'mongoose';
import { expectAssignable } from 'tsd';

const childSchema: Schema = new Schema({ name: String });

Expand Down Expand Up @@ -108,3 +117,32 @@ function gh13040(): void {
product.ownerDocument();
});
}

function gh14601() {
interface ISub {
field1: string;
}
interface IMain {
f1: string;
f2: HydratedSingleSubdocument<ISub>;
f3: HydratedArraySubdocument<ISub>[];
}

const subSchema = new Schema({ field1: String }, { _id: false });

const mainSchema = new Schema({
f1: String,
f2: { type: subSchema },
f3: { type: [subSchema] }
});
const MainModel = model<IMain>('Main', mainSchema);

const item = new MainModel({
f1: 'test',
f2: { field1: 'test' },
f3: [{ field1: 'test' }]
});

const f2obj = item.f2.toObject();
expectAssignable<{ _id: Types.ObjectId, field1: string }>(f2obj);
}
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ declare module 'mongoose' {
>
>
>;
export type HydratedSingleSubdocument<DocType, TOverrides = {}> = Types.Subdocument<unknown> & Require_id<DocType> & TOverrides;
export type HydratedArraySubdocument<DocType, TOverrides = {}> = Types.ArraySubdocument<unknown> & Require_id<DocType> & TOverrides;
export type HydratedSingleSubdocument<DocType, TOverrides = {}> = Types.Subdocument<unknown, Record<string, never>, DocType> & Require_id<DocType> & TOverrides;
export type HydratedArraySubdocument<DocType, TOverrides = {}> = Types.ArraySubdocument<unknown, Record<string, never>, DocType> & Require_id<DocType> & TOverrides;

export type HydratedDocumentFromSchema<TSchema extends Schema> = HydratedDocument<
InferSchemaType<TSchema>,
Expand Down
2 changes: 1 addition & 1 deletion types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ declare module 'mongoose' {
$parent(): Document;
}

class ArraySubdocument<IdType = any> extends Subdocument<IdType> {
class ArraySubdocument<IdType = any, TQueryHelpers = unknown, DocType = unknown> extends Subdocument<IdType, TQueryHelpers, DocType> {
/** Returns this sub-documents parent array. */
parentArray(): Types.DocumentArray<unknown>;
}
Expand Down